#data deposit

#rv sort and ffr sort and regression
rvsort<-data.frame()
for (q in 1:4) {
  ss_bdmi<-subset(BDMI_ps,BDMI_rvsort==q)
  ss_bdmlci<-subset(BDMI_ps,BDMLCI_rvsort==q)
  ss_bdmxlci<-subset(BDMI_ps,BDMXLCI_rvsort==q)
  ss_ps<-subset(BDMI_ps,ps_rvsort==q)
  rvsort<-rbind(rvsort,data.frame(q=q,BDMI_xret=mean(ss_bdmi$BDMI_logxret),BDMI_rvmgd=mean(ss_bdmi$BDMI_rvmgd),
                                  BDMLCI_xret=mean(ss_bdmlci$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss_bdmlci$BDMLCI_rvmgd),
                                  BDMXLCI_xret=mean(ss_bdmxlci$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss_bdmxlci$BDMXLCI_rvmgd),
                                  ps_xret=mean(ss_ps$ps_logxret),ps_rvmgd=mean(ss_ps$ps_rvmgd)))
}
rm(ss_bdmi,ss_bdmlci,ss_bdmxlci,ss_ps,q)

rvsort<-rbind(rvsort,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_ps, BDMI_rvsort == 4)$BDMI_logxret,subset(BDMI_ps, BDMI_rvsort == 1)$BDMI_logxret,alternative = "greater")$statistic,
                                BDMI_rvmgd=t.test(subset(BDMI_ps,BDMI_rvsort==4)$BDMI_rvmgd,subset(BDMI_ps,BDMI_rvsort==1)$BDMI_rvmgd,alternative = "greater")$statistic,
                                BDMLCI_xret=t.test(subset(BDMI_ps,BDMLCI_rvsort==4)$BDMLCI_logxret,subset(BDMI_ps,BDMLCI_rvsort==1)$BDMLCI_logxret,alternative = "greater")$statistic,
                                BDMLCI_rvmgd=t.test(subset(BDMI_ps,BDMLCI_rvsort==4)$BDMLCI_rvmgd,subset(BDMI_ps,BDMLCI_rvsort==1)$BDMLCI_rvmgd,alternative = "greater")$statistic,
                                BDMXLCI_xret=t.test(subset(BDMI_ps,BDMXLCI_rvsort==4)$BDMXLCI_logxret,subset(BDMI_ps,BDMXLCI_rvsort==1)$BDMXLCI_logxret,alternative = "greater")$statistic,
                                BDMXLCI_rvmgd=t.test(subset(BDMI_ps,BDMXLCI_rvsort==4)$BDMXLCI_rvmgd,subset(BDMI_ps,BDMXLCI_rvsort==1)$BDMXLCI_rvmgd,alternative = "greater")$statistic,
                                ps_xret=t.test(subset(BDMI_ps,ps_rvsort==4)$ps_logxret,subset(BDMI_ps,ps_rvsort==1)$ps_logxret,alternative = "greater")$statistic,
                                ps_rvmgd=t.test(subset(BDMI_ps,ps_rvsort==4)$ps_rvmgd,subset(BDMI_ps,ps_rvsort==1)$ps_rvmgd,alternative = "greater")$statistic))


ffrsort<-data.frame()
for (q in 1:2) {
  ss<-subset(BDMI_ps,ffrsort==q)
  ffrsort<-rbind(ffrsort,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),
                                    BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),
                                    BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),
                                    ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd)))
}
rm(ss,q)
ffrsort<-rbind(ffrsort,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_ps,ffrsort==2)$BDMI_logxret-subset(BDMI_ps,ffrsort==1)$BDMI_logxret)$statistic,
                                  BDMI_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$BDMI_rvmgd-subset(BDMI_ps,ffrsort==1)$BDMI_rvmgd)$statistic,
                                  BDMLCI_xret=t.test(subset(BDMI_ps,ffrsort==2)$BDMLCI_logxret-subset(BDMI_ps,ffrsort==1)$BDMLCI_logxret)$statistic,
                                  BDMLCI_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$BDMLCI_rvmgd-subset(BDMI_ps,ffrsort==1)$BDMLCI_rvmgd)$statistic,
                                  BDMXLCI_xret=t.test(subset(BDMI_ps,ffrsort==2)$BDMXLCI_logxret-subset(BDMI_ps,ffrsort==1)$BDMXLCI_logxret)$statistic,
                                  BDMXLCI_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$BDMXLCI_rvmgd-subset(BDMI_ps,ffrsort==1)$BDMXLCI_rvmgd)$statistic,
                                  ps_xret=t.test(subset(BDMI_ps,ffrsort==2)$ps_logxret-subset(BDMI_ps,ffrsort==1)$ps_logxret)$statistic,
                                  ps_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$ps_rvmgd-subset(BDMI_ps,ffrsort==1)$ps_rvmgd)$statistic))

ffrsort<-rbind(ffrsort,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_ps,ffrsort==2)$BDMI_logxret,subset(BDMI_ps,ffrsort==1)$BDMI_logxret,alternative = "greater")$statistic,
                                  BDMI_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$BDMI_rvmgd,subset(BDMI_ps,ffrsort==1)$BDMI_rvmgd,alternative = "greater")$statistic,
                                  BDMLCI_xret=t.test(subset(BDMI_ps,ffrsort==2)$BDMLCI_logxret,subset(BDMI_ps,ffrsort==1)$BDMLCI_logxret,alternative = "greater")$statistic,
                                  BDMLCI_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$BDMLCI_rvmgd,subset(BDMI_ps,ffrsort==1)$BDMLCI_rvmgd,alternative = "greater")$statistic,
                                  BDMXLCI_xret=t.test(subset(BDMI_ps,ffrsort==2)$BDMXLCI_logxret,subset(BDMI_ps,ffrsort==1)$BDMXLCI_logxret,alternative = "greater")$statistic,
                                  BDMXLCI_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$BDMXLCI_rvmgd,subset(BDMI_ps,ffrsort==1)$BDMXLCI_rvmgd,alternative = "greater")$statistic,
                                  ps_xret=t.test(subset(BDMI_ps,ffrsort==2)$ps_logxret,subset(BDMI_ps,ffrsort==1)$ps_logxret,alternative = "greater")$statistic,
                                  ps_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$ps_rvmgd,subset(BDMI_ps,ffrsort==1)$ps_rvmgd,alternative = "greater")$statistic))

#separate covid
#pre covid
BDMI_pre<-subset(BDMI_ps,month<202001|month>202112)
BDMI_pre$BDMI_rvsort<-cut(BDMI_pre$BDMI_realvar,breaks = quantile(BDMI_pre$BDMI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_pre$BDMLCI_rvsort<-cut(BDMI_pre$BDMLCI_realvar,breaks = quantile(BDMI_pre$BDMLCI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_pre$BDMXLCI_rvsort<-cut(BDMI_pre$BDMXLCI_realvar,breaks = quantile(BDMI_pre$BDMXLCI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_pre$ps_rvsort<-cut(BDMI_pre$ps_realvar,breaks = quantile(BDMI_pre$ps_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_pre$ffrsort<-cut(BDMI_pre$ffr,breaks = quantile(BDMI_pre$ffr,probs = seq(0,1,by=0.5),na.rm=T),include.lowest=TRUE,labels=FALSE)

rvsort_pre<-data.frame()
for (q in 1:4) {
  ss_bdmi<-subset(BDMI_pre,BDMI_rvsort==q)
  ss_bdmlci<-subset(BDMI_pre,BDMLCI_rvsort==q)
  ss_bdmxlci<-subset(BDMI_pre,BDMXLCI_rvsort==q)
  ss_ps<-subset(BDMI_pre,ps_rvsort==q)
  rvsort_pre<-rbind(rvsort_pre,data.frame(q=q,BDMI_xret=mean(ss_bdmi$BDMI_logxret),BDMI_rvmgd=mean(ss_bdmi$BDMI_rvmgd),
                                          BDMLCI_xret=mean(ss_bdmlci$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss_bdmlci$BDMLCI_rvmgd),
                                          BDMXLCI_xret=mean(ss_bdmxlci$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss_bdmxlci$BDMXLCI_rvmgd),
                                          ps_xret=mean(ss_ps$ps_logxret),ps_rvmgd=mean(ss_ps$ps_rvmgd)))
}
rm(ss_bdmi,ss_bdmlci,ss_bdmxlci,ss_ps,q)
rvsort_pre<-rbind(rvsort_pre,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_pre,BDMI_rvsort==4)$BDMI_logxret-subset(BDMI_pre,BDMI_rvsort==1)$BDMI_logxret)$statistic,
                                        BDMI_rvmgd=t.test(subset(BDMI_pre,BDMI_rvsort==4)$BDMI_rvmgd-subset(BDMI_pre,BDMI_rvsort==1)$BDMI_rvmgd)$statistic,
                                        BDMLCI_xret=t.test(subset(BDMI_pre,BDMLCI_rvsort==4)$BDMLCI_logxret-subset(BDMI_pre,BDMLCI_rvsort==1)$BDMLCI_logxret)$statistic,
                                        BDMLCI_rvmgd=t.test(subset(BDMI_pre,BDMLCI_rvsort==4)$BDMLCI_rvmgd-subset(BDMI_pre,BDMLCI_rvsort==1)$BDMLCI_rvmgd)$statistic,
                                        BDMXLCI_xret=t.test(subset(BDMI_pre,BDMXLCI_rvsort==4)$BDMXLCI_logxret-subset(BDMI_pre,BDMXLCI_rvsort==1)$BDMXLCI_logxret)$statistic,
                                        BDMXLCI_rvmgd=t.test(subset(BDMI_pre,BDMXLCI_rvsort==4)$BDMXLCI_rvmgd-subset(BDMI_pre,BDMXLCI_rvsort==1)$BDMXLCI_rvmgd)$statistic,
                                        ps_xret=t.test(subset(BDMI_pre,ps_rvsort==4)$ps_logxret-subset(BDMI_pre,ps_rvsort==1)$ps_logxret)$statistic,
                                        ps_rvmgd=t.test(subset(BDMI_pre,ps_rvsort==4)$ps_rvmgd-subset(BDMI_pre,ps_rvsort==1)$ps_rvmgd)$statistic))

ffrsort_pre<-data.frame()
for (q in 1:2) {
  ss<-subset(BDMI_pre,ffrsort==q)
  ffrsort_pre<-rbind(ffrsort_pre,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),
                                            BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),
                                            BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),
                                            ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd)))
}
rm(ss,q)
ffrsort_pre<-rbind(ffrsort_pre,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_pre,ffrsort==2)$BDMI_logxret-subset(BDMI_pre,ffrsort==1)$BDMI_logxret)$statistic,
                                          BDMI_rvmgd=t.test(subset(BDMI_pre,ffrsort==2)$BDMI_rvmgd-subset(BDMI_pre,ffrsort==1)$BDMI_rvmgd)$statistic,
                                          BDMLCI_xret=t.test(subset(BDMI_pre,ffrsort==2)$BDMLCI_logxret-subset(BDMI_pre,ffrsort==1)$BDMLCI_logxret)$statistic,
                                          BDMLCI_rvmgd=t.test(subset(BDMI_pre,ffrsort==2)$BDMLCI_rvmgd-subset(BDMI_pre,ffrsort==1)$BDMLCI_rvmgd)$statistic,
                                          BDMXLCI_xret=t.test(subset(BDMI_pre,ffrsort==2)$BDMXLCI_logxret-subset(BDMI_pre,ffrsort==1)$BDMXLCI_logxret)$statistic,
                                          BDMXLCI_rvmgd=t.test(subset(BDMI_pre,ffrsort==2)$BDMXLCI_rvmgd-subset(BDMI_pre,ffrsort==1)$BDMXLCI_rvmgd)$statistic,
                                          ps_xret=t.test(subset(BDMI_pre,ffrsort==2)$ps_logxret-subset(BDMI_pre,ffrsort==1)$ps_logxret)$statistic,
                                          ps_rvmgd=t.test(subset(BDMI_pre,ffrsort==2)$ps_rvmgd-subset(BDMI_pre,ffrsort==1)$ps_rvmgd)$statistic))

#covid
BDMI_post<-subset(BDMI_ps,month>=202001&month<=202112)
BDMI_post$BDMI_rvsort<-cut(BDMI_post$BDMI_realvar,breaks = quantile(BDMI_post$BDMI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_post$BDMLCI_rvsort<-cut(BDMI_post$BDMLCI_realvar,breaks = quantile(BDMI_post$BDMLCI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_post$BDMXLCI_rvsort<-cut(BDMI_post$BDMXLCI_realvar,breaks = quantile(BDMI_post$BDMXLCI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_post$ps_rvsort<-cut(BDMI_post$ps_realvar,breaks = quantile(BDMI_post$ps_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)

BDMI_post$ffrsort<-cut(BDMI_post$ffr,breaks = quantile(BDMI_post$ffr,probs = seq(0,1,by=0.5),na.rm=T),include.lowest=TRUE,labels=FALSE)
#BDMI_post$ffrsort <- cut(BDMI_post$ffr, breaks = seq(min(BDMI_post$ffr, na.rm = TRUE), max(BDMI_post$ffr, na.rm = TRUE), length.out = 5), include.lowest = TRUE, labels = FALSE)

rvsort_post<-data.frame()
for (q in 1:4) {
  ss_bdmi<-subset(BDMI_post,BDMI_rvsort==q)
  ss_bdmlci<-subset(BDMI_post,BDMLCI_rvsort==q)
  ss_bdmxlci<-subset(BDMI_post,BDMXLCI_rvsort==q)
  ss_ps<-subset(BDMI_post,ps_rvsort==q)
  rvsort_post<-rbind(rvsort_post,data.frame(q=q,BDMI_xret=mean(ss_bdmi$BDMI_logxret),BDMI_rvmgd=mean(ss_bdmi$BDMI_rvmgd),
                                            BDMLCI_xret=mean(ss_bdmlci$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss_bdmlci$BDMLCI_rvmgd),
                                            BDMXLCI_xret=mean(ss_bdmxlci$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss_bdmxlci$BDMXLCI_rvmgd),
                                            ps_xret=mean(ss_ps$ps_logxret),ps_rvmgd=mean(ss_ps$ps_rvmgd)))
}
rm(ss_bdmi,ss_bdmlci,ss_bdmxlci,ss_ps,q)

rvsort_post<-rbind(rvsort_post,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_post,BDMI_rvsort==4)$BDMI_logxret-subset(BDMI_post,BDMI_rvsort==1)$BDMI_logxret)$statistic,
                                          BDMI_rvmgd=t.test(subset(BDMI_post,BDMI_rvsort==4)$BDMI_rvmgd-subset(BDMI_post,BDMI_rvsort==1)$BDMI_rvmgd)$statistic,
                                          BDMLCI_xret=t.test(subset(BDMI_post,BDMLCI_rvsort==4)$BDMLCI_logxret-subset(BDMI_post,BDMLCI_rvsort==1)$BDMLCI_logxret)$statistic,
                                          BDMLCI_rvmgd=t.test(subset(BDMI_post,BDMLCI_rvsort==4)$BDMLCI_rvmgd-subset(BDMI_post,BDMLCI_rvsort==1)$BDMLCI_rvmgd)$statistic,
                                          BDMXLCI_xret=t.test(subset(BDMI_post,BDMXLCI_rvsort==4)$BDMXLCI_logxret-subset(BDMI_post,BDMXLCI_rvsort==1)$BDMXLCI_logxret)$statistic,
                                          BDMXLCI_rvmgd=t.test(subset(BDMI_post,BDMXLCI_rvsort==4)$BDMXLCI_rvmgd-subset(BDMI_post,BDMXLCI_rvsort==1)$BDMXLCI_rvmgd)$statistic,
                                          ps_xret=t.test(subset(BDMI_post,ps_rvsort==4)$ps_logxret-subset(BDMI_post,ps_rvsort==1)$ps_logxret)$statistic,
                                          ps_rvmgd=t.test(subset(BDMI_post,ps_rvsort==4)$ps_rvmgd-subset(BDMI_post,ps_rvsort==1)$ps_rvmgd)$statistic))

ffrsort_post<-data.frame()
for (q in 1:2) {
  ss<-subset(BDMI_post,ffrsort==q)
  ffrsort_post<-rbind(ffrsort_post,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),
                                              BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),
                                              BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),
                                              ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd)))
}
rm(ss,q)
ffrsort_post<-rbind(ffrsort_post,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_post,ffrsort==2)$BDMI_logxret-subset(BDMI_post,ffrsort==1)$BDMI_logxret)$statistic,
                                            BDMI_rvmgd=t.test(subset(BDMI_post,ffrsort==2)$BDMI_rvmgd-subset(BDMI_post,ffrsort==1)$BDMI_rvmgd)$statistic,
                                            BDMLCI_xret=t.test(subset(BDMI_post,ffrsort==2)$BDMLCI_logxret-subset(BDMI_post,ffrsort==1)$BDMLCI_logxret)$statistic,
                                            BDMLCI_rvmgd=t.test(subset(BDMI_post,ffrsort==2)$BDMLCI_rvmgd-subset(BDMI_post,ffrsort==1)$BDMLCI_rvmgd)$statistic,
                                            BDMXLCI_xret=t.test(subset(BDMI_post,ffrsort==2)$BDMXLCI_logxret-subset(BDMI_post,ffrsort==1)$BDMXLCI_logxret)$statistic,
                                            BDMXLCI_rvmgd=t.test(subset(BDMI_post,ffrsort==2)$BDMXLCI_rvmgd-subset(BDMI_post,ffrsort==1)$BDMXLCI_rvmgd)$statistic,
                                            ps_xret=t.test(subset(BDMI_post,ffrsort==2)$ps_logxret-subset(BDMI_post,ffrsort==1)$ps_logxret)$statistic,
                                            ps_rvmgd=t.test(subset(BDMI_post,ffrsort==2)$ps_rvmgd-subset(BDMI_post,ffrsort==1)$ps_rvmgd)$statistic))




#double sort controlling ffr
ffrrvsort<-data.frame()
for (qffr in 1:2) {
  ss<-subset(BDMI_ps,ffrsort==qffr)
  ss$ffrrv_bdmi<-cut(ss$BDMI_realvar,breaks = quantile(ss$BDMI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
  ss$ffrrv_bdmlci<-cut(ss$BDMLCI_realvar,breaks = quantile(ss$BDMLCI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
  ss$ffrrv_bdmxlci<-cut(ss$BDMXLCI_realvar,breaks = quantile(ss$BDMXLCI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
  ss$ffrrv_ps<-cut(ss$ps_realvar,breaks = quantile(ss$ps_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
  for (qrv in 1:4) {
    ssss_bdmi<-subset(ss,ffrrv_bdmi==qrv)
    ssss_bdmlci<-subset(ss,ffrrv_bdmlci==qrv)
    ssss_bdmxlci<-subset(ss,ffrrv_bdmxlci==qrv)
    ssss_ps<-subset(ss,ffrrv_ps==qrv)
    ffrrvsort<-rbind(ffrrvsort,data.frame(qffr=qffr,qrv=qrv,BDMI_xret=mean(ssss_bdmi$BDMI_logxret),BDMI_rvmgd=mean(ssss_bdmi$BDMI_rvmgd),
                                          BDMLCI_xret=mean(ssss_bdmlci$BDMLCI_logxret),BDMLCI_rvmgd=mean(ssss_bdmlci$BDMLCI_rvmgd),
                                          BDMXLCI_xret=mean(ssss_bdmxlci$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ssss_bdmxlci$BDMXLCI_rvmgd),
                                          ps_xret=mean(ssss_ps$ps_logxret),ps_rvmgd=mean(ssss_ps$ps_rvmgd)))
  }
}
rm(qffr,qrv,ss,ssss_bdmi,ssss_bdmlci,ssss_bdmxlci,ssss_ps)

#regression within each ffr quartile
ffrregresult <- data.frame()
for (qffr in 1:2) {
  ss <- subset(BDMI_ps, ffrsort == qffr)
  regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
  regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
  regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
  regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
  ffrregresult <- rbind(ffrregresult, data.frame(q = qffr,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(BDMI_ps$BDMI_rvmgd ~ BDMI_ps$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_ps$BDMLCI_rvmgd ~ BDMI_ps$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_ps$BDMXLCI_rvmgd ~ BDMI_ps$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_ps$ps_rvmgd ~ BDMI_ps$ps_logxret)
ffrregresult <- rbind(ffrregresult, data.frame(q ="fs",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(qffr,ss,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#pre-covid
for (qffr in 1:2) {
  ss <- subset(BDMI_pre, ffrsort == qffr)
  if (nrow(ss)>0) {
    regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
    regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
    regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
    regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
    ffrregresult <- rbind(ffrregresult, data.frame(q = qffr,
                                                   alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                   stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                   p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                   beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                   stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                   p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                   adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                   ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                   
                                                   alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                   stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                   p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                   beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                   stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                   p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                   adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                   ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                   
                                                   alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                   stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                   p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                   beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                   stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                   p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                   adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                   ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                   
                                                   alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                   stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                   p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                   beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                   stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                   p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                   adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                   ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
  }
}

regresult_bdmi <- lm(BDMI_pre$BDMI_rvmgd ~ BDMI_pre$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_pre$BDMLCI_rvmgd ~ BDMI_pre$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_pre$BDMXLCI_rvmgd ~ BDMI_pre$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_pre$ps_rvmgd ~ BDMI_pre$ps_logxret)
ffrregresult <- rbind(ffrregresult, data.frame(q ="fs_pre",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(qffr,ss,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#covid
for (qffr in 1:2) {
  ss <- subset(BDMI_post, ffrsort == qffr)
  if (nrow(ss)>0) {
    regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
    regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
    regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
    regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
    ffrregresult <- rbind(ffrregresult, data.frame(q = qffr,
                                                   alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                   stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                   p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                   beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                   stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                   p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                   adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                   ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                   
                                                   alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                   stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                   p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                   beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                   stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                   p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                   adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                   ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                   
                                                   alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                   stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                   p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                   beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                   stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                   p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                   adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                   ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                   
                                                   alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                   stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                   p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                   beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                   stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                   p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                   adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                   ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
  }
}
regresult_bdmi <- lm(BDMI_post$BDMI_rvmgd ~ BDMI_post$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_post$BDMLCI_rvmgd ~ BDMI_post$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_post$BDMXLCI_rvmgd ~ BDMI_post$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_post$ps_rvmgd ~ BDMI_post$ps_logxret)
ffrregresult <- rbind(ffrregresult, data.frame(q ="fs_post",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(qffr,ss,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)






#rv and ffr sort
rvsort<-data.frame()
for (q in 1:4) {
  ss_bdmi<-subset(BDMI_ps,BDMI_rvsort==q)
  ss_bdmlci<-subset(BDMI_ps,BDMLCI_rvsort==q)
  ss_bdmxlci<-subset(BDMI_ps,BDMXLCI_rvsort==q)
  ss_ps<-subset(BDMI_ps,ps_rvsort==q)
  rvsort<-rbind(rvsort,data.frame(q=q,BDMI_xret=mean(ss_bdmi$BDMI_logxret),BDMI_rvmgd=mean(ss_bdmi$BDMI_rvmgd),
                                  BDMLCI_xret=mean(ss_bdmlci$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss_bdmlci$BDMLCI_rvmgd),
                                  BDMXLCI_xret=mean(ss_bdmxlci$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss_bdmxlci$BDMXLCI_rvmgd),
                                  ps_xret=mean(ss_ps$ps_logxret),ps_rvmgd=mean(ss_ps$ps_rvmgd)))
}
rm(ss_bdmi,ss_bdmlci,ss_bdmxlci,ss_ps,q)


rvsort<-rbind(rvsort,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_ps, BDMI_rvsort == 4)$BDMI_logxret,subset(BDMI_ps, BDMI_rvsort == 1)$BDMI_logxret,alternative = "greater")$statistic,
                                BDMI_rvmgd=t.test(subset(BDMI_ps,BDMI_rvsort==4)$BDMI_rvmgd,subset(BDMI_ps,BDMI_rvsort==1)$BDMI_rvmgd,alternative = "greater")$statistic,
                                BDMLCI_xret=t.test(subset(BDMI_ps,BDMLCI_rvsort==4)$BDMLCI_logxret,subset(BDMI_ps,BDMLCI_rvsort==1)$BDMLCI_logxret,alternative = "greater")$statistic,
                                BDMLCI_rvmgd=t.test(subset(BDMI_ps,BDMLCI_rvsort==4)$BDMLCI_rvmgd,subset(BDMI_ps,BDMLCI_rvsort==1)$BDMLCI_rvmgd,alternative = "greater")$statistic,
                                BDMXLCI_xret=t.test(subset(BDMI_ps,BDMXLCI_rvsort==4)$BDMXLCI_logxret,subset(BDMI_ps,BDMXLCI_rvsort==1)$BDMXLCI_logxret,alternative = "greater")$statistic,
                                BDMXLCI_rvmgd=t.test(subset(BDMI_ps,BDMXLCI_rvsort==4)$BDMXLCI_rvmgd,subset(BDMI_ps,BDMXLCI_rvsort==1)$BDMXLCI_rvmgd,alternative = "greater")$statistic,
                                ps_xret=t.test(subset(BDMI_ps,ps_rvsort==4)$ps_logxret,subset(BDMI_ps,ps_rvsort==1)$ps_logxret,alternative = "greater")$statistic,
                                ps_rvmgd=t.test(subset(BDMI_ps,ps_rvsort==4)$ps_rvmgd,subset(BDMI_ps,ps_rvsort==1)$ps_rvmgd,alternative = "greater")$statistic))


ffrsort<-data.frame()
for (q in 1:2) {
  ss<-subset(BDMI_ps,ffrsort==q)
  ffrsort<-rbind(ffrsort,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),
                                    BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),
                                    BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),
                                    ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd)))
}
rm(ss,q)
ffrsort<-rbind(ffrsort,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_ps,ffrsort==2)$BDMI_logxret-subset(BDMI_ps,ffrsort==1)$BDMI_logxret)$statistic,
                                  BDMI_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$BDMI_rvmgd-subset(BDMI_ps,ffrsort==1)$BDMI_rvmgd)$statistic,
                                  BDMLCI_xret=t.test(subset(BDMI_ps,ffrsort==2)$BDMLCI_logxret-subset(BDMI_ps,ffrsort==1)$BDMLCI_logxret)$statistic,
                                  BDMLCI_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$BDMLCI_rvmgd-subset(BDMI_ps,ffrsort==1)$BDMLCI_rvmgd)$statistic,
                                  BDMXLCI_xret=t.test(subset(BDMI_ps,ffrsort==2)$BDMXLCI_logxret-subset(BDMI_ps,ffrsort==1)$BDMXLCI_logxret)$statistic,
                                  BDMXLCI_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$BDMXLCI_rvmgd-subset(BDMI_ps,ffrsort==1)$BDMXLCI_rvmgd)$statistic,
                                  ps_xret=t.test(subset(BDMI_ps,ffrsort==2)$ps_logxret-subset(BDMI_ps,ffrsort==1)$ps_logxret)$statistic,
                                  ps_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$ps_rvmgd-subset(BDMI_ps,ffrsort==1)$ps_rvmgd)$statistic))

ffrsort<-rbind(ffrsort,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_ps,ffrsort==2)$BDMI_logxret,subset(BDMI_ps,ffrsort==1)$BDMI_logxret,alternative = "greater")$statistic,
                                  BDMI_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$BDMI_rvmgd,subset(BDMI_ps,ffrsort==1)$BDMI_rvmgd,alternative = "greater")$statistic,
                                  BDMLCI_xret=t.test(subset(BDMI_ps,ffrsort==2)$BDMLCI_logxret,subset(BDMI_ps,ffrsort==1)$BDMLCI_logxret,alternative = "greater")$statistic,
                                  BDMLCI_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$BDMLCI_rvmgd,subset(BDMI_ps,ffrsort==1)$BDMLCI_rvmgd,alternative = "greater")$statistic,
                                  BDMXLCI_xret=t.test(subset(BDMI_ps,ffrsort==2)$BDMXLCI_logxret,subset(BDMI_ps,ffrsort==1)$BDMXLCI_logxret,alternative = "greater")$statistic,
                                  BDMXLCI_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$BDMXLCI_rvmgd,subset(BDMI_ps,ffrsort==1)$BDMXLCI_rvmgd,alternative = "greater")$statistic,
                                  ps_xret=t.test(subset(BDMI_ps,ffrsort==2)$ps_logxret,subset(BDMI_ps,ffrsort==1)$ps_logxret,alternative = "greater")$statistic,
                                  ps_rvmgd=t.test(subset(BDMI_ps,ffrsort==2)$ps_rvmgd,subset(BDMI_ps,ffrsort==1)$ps_rvmgd,alternative = "greater")$statistic))

#separate covid
#pre covid
BDMI_pre<-subset(BDMI_ps,month<202001|month>202112)
BDMI_pre$BDMI_rvsort<-cut(BDMI_pre$BDMI_realvar,breaks = quantile(BDMI_pre$BDMI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_pre$BDMLCI_rvsort<-cut(BDMI_pre$BDMLCI_realvar,breaks = quantile(BDMI_pre$BDMLCI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_pre$BDMXLCI_rvsort<-cut(BDMI_pre$BDMXLCI_realvar,breaks = quantile(BDMI_pre$BDMXLCI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_pre$ps_rvsort<-cut(BDMI_pre$ps_realvar,breaks = quantile(BDMI_pre$ps_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_pre$ffrsort<-cut(BDMI_pre$ffr,breaks = quantile(BDMI_pre$ffr,probs = seq(0,1,by=0.5),na.rm=T),include.lowest=TRUE,labels=FALSE)

rvsort_pre<-data.frame()
for (q in 1:4) {
  ss_bdmi<-subset(BDMI_pre,BDMI_rvsort==q)
  ss_bdmlci<-subset(BDMI_pre,BDMLCI_rvsort==q)
  ss_bdmxlci<-subset(BDMI_pre,BDMXLCI_rvsort==q)
  ss_ps<-subset(BDMI_pre,ps_rvsort==q)
  rvsort_pre<-rbind(rvsort_pre,data.frame(q=q,BDMI_xret=mean(ss_bdmi$BDMI_logxret),BDMI_rvmgd=mean(ss_bdmi$BDMI_rvmgd),
                                          BDMLCI_xret=mean(ss_bdmlci$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss_bdmlci$BDMLCI_rvmgd),
                                          BDMXLCI_xret=mean(ss_bdmxlci$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss_bdmxlci$BDMXLCI_rvmgd),
                                          ps_xret=mean(ss_ps$ps_logxret),ps_rvmgd=mean(ss_ps$ps_rvmgd)))
}
rm(ss_bdmi,ss_bdmlci,ss_bdmxlci,ss_ps,q)
rvsort_pre<-rbind(rvsort_pre,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_pre,BDMI_rvsort==4)$BDMI_logxret-subset(BDMI_pre,BDMI_rvsort==1)$BDMI_logxret)$statistic,
                                        BDMI_rvmgd=t.test(subset(BDMI_pre,BDMI_rvsort==4)$BDMI_rvmgd-subset(BDMI_pre,BDMI_rvsort==1)$BDMI_rvmgd)$statistic,
                                        BDMLCI_xret=t.test(subset(BDMI_pre,BDMLCI_rvsort==4)$BDMLCI_logxret-subset(BDMI_pre,BDMLCI_rvsort==1)$BDMLCI_logxret)$statistic,
                                        BDMLCI_rvmgd=t.test(subset(BDMI_pre,BDMLCI_rvsort==4)$BDMLCI_rvmgd-subset(BDMI_pre,BDMLCI_rvsort==1)$BDMLCI_rvmgd)$statistic,
                                        BDMXLCI_xret=t.test(subset(BDMI_pre,BDMXLCI_rvsort==4)$BDMXLCI_logxret-subset(BDMI_pre,BDMXLCI_rvsort==1)$BDMXLCI_logxret)$statistic,
                                        BDMXLCI_rvmgd=t.test(subset(BDMI_pre,BDMXLCI_rvsort==4)$BDMXLCI_rvmgd-subset(BDMI_pre,BDMXLCI_rvsort==1)$BDMXLCI_rvmgd)$statistic,
                                        ps_xret=t.test(subset(BDMI_pre,ps_rvsort==4)$ps_logxret-subset(BDMI_pre,ps_rvsort==1)$ps_logxret)$statistic,
                                        ps_rvmgd=t.test(subset(BDMI_pre,ps_rvsort==4)$ps_rvmgd-subset(BDMI_pre,ps_rvsort==1)$ps_rvmgd)$statistic))

ffrsort_pre<-data.frame()
for (q in 1:2) {
  ss<-subset(BDMI_pre,ffrsort==q)
  ffrsort_pre<-rbind(ffrsort_pre,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),
                                            BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),
                                            BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),
                                            ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd)))
}
rm(ss,q)
ffrsort_pre<-rbind(ffrsort_pre,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_pre,ffrsort==2)$BDMI_logxret-subset(BDMI_pre,ffrsort==1)$BDMI_logxret)$statistic,
                                          BDMI_rvmgd=t.test(subset(BDMI_pre,ffrsort==2)$BDMI_rvmgd-subset(BDMI_pre,ffrsort==1)$BDMI_rvmgd)$statistic,
                                          BDMLCI_xret=t.test(subset(BDMI_pre,ffrsort==2)$BDMLCI_logxret-subset(BDMI_pre,ffrsort==1)$BDMLCI_logxret)$statistic,
                                          BDMLCI_rvmgd=t.test(subset(BDMI_pre,ffrsort==2)$BDMLCI_rvmgd-subset(BDMI_pre,ffrsort==1)$BDMLCI_rvmgd)$statistic,
                                          BDMXLCI_xret=t.test(subset(BDMI_pre,ffrsort==2)$BDMXLCI_logxret-subset(BDMI_pre,ffrsort==1)$BDMXLCI_logxret)$statistic,
                                          BDMXLCI_rvmgd=t.test(subset(BDMI_pre,ffrsort==2)$BDMXLCI_rvmgd-subset(BDMI_pre,ffrsort==1)$BDMXLCI_rvmgd)$statistic,
                                          ps_xret=t.test(subset(BDMI_pre,ffrsort==2)$ps_logxret-subset(BDMI_pre,ffrsort==1)$ps_logxret)$statistic,
                                          ps_rvmgd=t.test(subset(BDMI_pre,ffrsort==2)$ps_rvmgd-subset(BDMI_pre,ffrsort==1)$ps_rvmgd)$statistic))

#covid
BDMI_post<-subset(BDMI_ps,month>=202001&month<=202112)
BDMI_post$BDMI_rvsort<-cut(BDMI_post$BDMI_realvar,breaks = quantile(BDMI_post$BDMI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_post$BDMLCI_rvsort<-cut(BDMI_post$BDMLCI_realvar,breaks = quantile(BDMI_post$BDMLCI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_post$BDMXLCI_rvsort<-cut(BDMI_post$BDMXLCI_realvar,breaks = quantile(BDMI_post$BDMXLCI_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)
BDMI_post$ps_rvsort<-cut(BDMI_post$ps_realvar,breaks = quantile(BDMI_post$ps_realvar,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)

BDMI_post$ffrsort<-cut(BDMI_post$ffr,breaks = quantile(BDMI_post$ffr,probs = seq(0,1,by=0.5),na.rm=T),include.lowest=TRUE,labels=FALSE)
#BDMI_post$ffrsort <- cut(BDMI_post$ffr, breaks = seq(min(BDMI_post$ffr, na.rm = TRUE), max(BDMI_post$ffr, na.rm = TRUE), length.out = 5), include.lowest = TRUE, labels = FALSE)

rvsort_post<-data.frame()
for (q in 1:4) {
  ss_bdmi<-subset(BDMI_post,BDMI_rvsort==q)
  ss_bdmlci<-subset(BDMI_post,BDMLCI_rvsort==q)
  ss_bdmxlci<-subset(BDMI_post,BDMXLCI_rvsort==q)
  ss_ps<-subset(BDMI_post,ps_rvsort==q)
  rvsort_post<-rbind(rvsort_post,data.frame(q=q,BDMI_xret=mean(ss_bdmi$BDMI_logxret),BDMI_rvmgd=mean(ss_bdmi$BDMI_rvmgd),
                                            BDMLCI_xret=mean(ss_bdmlci$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss_bdmlci$BDMLCI_rvmgd),
                                            BDMXLCI_xret=mean(ss_bdmxlci$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss_bdmxlci$BDMXLCI_rvmgd),
                                            ps_xret=mean(ss_ps$ps_logxret),ps_rvmgd=mean(ss_ps$ps_rvmgd)))
}
rm(ss_bdmi,ss_bdmlci,ss_bdmxlci,ss_ps,q)

rvsort_post<-rbind(rvsort_post,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_post,BDMI_rvsort==4)$BDMI_logxret-subset(BDMI_post,BDMI_rvsort==1)$BDMI_logxret)$statistic,
                                          BDMI_rvmgd=t.test(subset(BDMI_post,BDMI_rvsort==4)$BDMI_rvmgd-subset(BDMI_post,BDMI_rvsort==1)$BDMI_rvmgd)$statistic,
                                          BDMLCI_xret=t.test(subset(BDMI_post,BDMLCI_rvsort==4)$BDMLCI_logxret-subset(BDMI_post,BDMLCI_rvsort==1)$BDMLCI_logxret)$statistic,
                                          BDMLCI_rvmgd=t.test(subset(BDMI_post,BDMLCI_rvsort==4)$BDMLCI_rvmgd-subset(BDMI_post,BDMLCI_rvsort==1)$BDMLCI_rvmgd)$statistic,
                                          BDMXLCI_xret=t.test(subset(BDMI_post,BDMXLCI_rvsort==4)$BDMXLCI_logxret-subset(BDMI_post,BDMXLCI_rvsort==1)$BDMXLCI_logxret)$statistic,
                                          BDMXLCI_rvmgd=t.test(subset(BDMI_post,BDMXLCI_rvsort==4)$BDMXLCI_rvmgd-subset(BDMI_post,BDMXLCI_rvsort==1)$BDMXLCI_rvmgd)$statistic,
                                          ps_xret=t.test(subset(BDMI_post,ps_rvsort==4)$ps_logxret-subset(BDMI_post,ps_rvsort==1)$ps_logxret)$statistic,
                                          ps_rvmgd=t.test(subset(BDMI_post,ps_rvsort==4)$ps_rvmgd-subset(BDMI_post,ps_rvsort==1)$ps_rvmgd)$statistic))

ffrsort_post<-data.frame()
for (q in 1:2) {
  ss<-subset(BDMI_post,ffrsort==q)
  ffrsort_post<-rbind(ffrsort_post,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),
                                              BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),
                                              BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),
                                              ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd)))
}
rm(ss,q)
ffrsort_post<-rbind(ffrsort_post,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_post,ffrsort==2)$BDMI_logxret-subset(BDMI_post,ffrsort==1)$BDMI_logxret)$statistic,
                                            BDMI_rvmgd=t.test(subset(BDMI_post,ffrsort==2)$BDMI_rvmgd-subset(BDMI_post,ffrsort==1)$BDMI_rvmgd)$statistic,
                                            BDMLCI_xret=t.test(subset(BDMI_post,ffrsort==2)$BDMLCI_logxret-subset(BDMI_post,ffrsort==1)$BDMLCI_logxret)$statistic,
                                            BDMLCI_rvmgd=t.test(subset(BDMI_post,ffrsort==2)$BDMLCI_rvmgd-subset(BDMI_post,ffrsort==1)$BDMLCI_rvmgd)$statistic,
                                            BDMXLCI_xret=t.test(subset(BDMI_post,ffrsort==2)$BDMXLCI_logxret-subset(BDMI_post,ffrsort==1)$BDMXLCI_logxret)$statistic,
                                            BDMXLCI_rvmgd=t.test(subset(BDMI_post,ffrsort==2)$BDMXLCI_rvmgd-subset(BDMI_post,ffrsort==1)$BDMXLCI_rvmgd)$statistic,
                                            ps_xret=t.test(subset(BDMI_post,ffrsort==2)$ps_logxret-subset(BDMI_post,ffrsort==1)$ps_logxret)$statistic,
                                            ps_rvmgd=t.test(subset(BDMI_post,ffrsort==2)$ps_rvmgd-subset(BDMI_post,ffrsort==1)$ps_rvmgd)$statistic))




#ffr regression
ffrregresult <- data.frame()
for (qffr in 1:2) {
  ss <- subset(BDMI_ps, ffrsort == qffr)
  regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
  regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
  regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
  regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
  ffrregresult <- rbind(ffrregresult, data.frame(q = qffr,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(BDMI_ps$BDMI_rvmgd ~ BDMI_ps$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_ps$BDMLCI_rvmgd ~ BDMI_ps$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_ps$BDMXLCI_rvmgd ~ BDMI_ps$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_ps$ps_rvmgd ~ BDMI_ps$ps_logxret)
ffrregresult <- rbind(ffrregresult, data.frame(q ="fs",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(qffr,ss,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#pre-covid
for (qffr in 1:2) {
  ss <- subset(BDMI_pre, ffrsort == qffr)
  if (nrow(ss)>0) {
    regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
    regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
    regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
    regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
    ffrregresult <- rbind(ffrregresult, data.frame(q = qffr,
                                                   alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                   stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                   p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                   beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                   stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                   p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                   adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                   ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                   
                                                   alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                   stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                   p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                   beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                   stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                   p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                   adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                   ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                   
                                                   alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                   stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                   p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                   beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                   stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                   p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                   adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                   ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                   
                                                   alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                   stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                   p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                   beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                   stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                   p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                   adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                   ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
  }
}

regresult_bdmi <- lm(BDMI_pre$BDMI_rvmgd ~ BDMI_pre$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_pre$BDMLCI_rvmgd ~ BDMI_pre$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_pre$BDMXLCI_rvmgd ~ BDMI_pre$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_pre$ps_rvmgd ~ BDMI_pre$ps_logxret)
ffrregresult <- rbind(ffrregresult, data.frame(q ="fs_pre",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(qffr,ss,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#covid
for (qffr in 1:2) {
  ss <- subset(BDMI_post, ffrsort == qffr)
  if (nrow(ss)>0) {
    regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
    regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
    regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
    regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
    ffrregresult <- rbind(ffrregresult, data.frame(q = qffr,
                                                   alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                   stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                   p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                   beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                   stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                   p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                   adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                   ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                   
                                                   alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                   stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                   p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                   beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                   stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                   p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                   adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                   ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                   
                                                   alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                   stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                   p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                   beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                   stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                   p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                   adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                   ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                   
                                                   alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                   stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                   p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                   beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                   stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                   p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                   adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                   ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
  }
}
regresult_bdmi <- lm(BDMI_post$BDMI_rvmgd ~ BDMI_post$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_post$BDMLCI_rvmgd ~ BDMI_post$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_post$BDMXLCI_rvmgd ~ BDMI_post$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_post$ps_rvmgd ~ BDMI_post$ps_logxret)
ffrregresult <- rbind(ffrregresult, data.frame(q ="fs_post",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))










#fgi sort and regression
fgisort<-data.frame()
for (q in c("Extreme Fear","Fear","Neutral","Greed","Extreme Greed")) {
  ss<-subset(BDMI_ps,fng_classification==q)
  fgisort<-rbind(fgisort,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),BDMI_max=mean(ss$BDMIxret_max),
                                    BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),BDMLCI_max=mean(ss$BDMLCIxret_max),
                                    BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),BDMXLCI_max=mean(ss$BDMXLCIxret_max),
                                    ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd),ps_max=mean(ss$psxret_max)))
}
rm(ss,q)
fgisort<-rbind(fgisort,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMI_logxret-subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMI_logxret)$statistic,
                                  BDMI_rvmgd=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMI_rvmgd-subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMI_rvmgd)$statistic,
                                  BDMI_max=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMIxret_max-subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMIxret_max)$statistic,
                                  BDMLCI_xret=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMLCI_logxret-subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMLCI_logxret)$statistic,
                                  BDMLCI_rvmgd=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMLCI_rvmgd-subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMLCI_rvmgd)$statistic,
                                  BDMLCI_max=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMLCIxret_max-subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMLCIxret_max)$statistic,
                                  BDMXLCI_xret=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMXLCI_logxret-subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMXLCI_logxret)$statistic,
                                  BDMXLCI_rvmgd=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMXLCI_rvmgd-subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMXLCI_rvmgd)$statistic,
                                  BDMXLCI_max=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMXLCIxret_max-subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMXLCIxret_max)$statistic,
                                  ps_xret=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$ps_logxret-subset(BDMI_ps,fng_classification=="Extreme Fear")$ps_logxret)$statistic,
                                  ps_rvmgd=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$ps_rvmgd-subset(BDMI_ps,fng_classification=="Extreme Fear")$ps_rvmgd)$statistic,
                                  ps_max=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$psxret_max-subset(BDMI_ps,fng_classification=="Extreme Fear")$psxret_max)$statistic))

fgisort<-rbind(fgisort,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMI_logxret,subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMI_logxret,alternative = "greater")$statistic,
                                  BDMI_rvmgd=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMI_rvmgd,subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMI_rvmgd,alternative = "greater")$statistic,
                                  BDMI_max=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMIxret_max,subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMIxret_max,alternative = "greater")$statistic,
                                  BDMLCI_xret=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMLCI_logxret,subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMLCI_logxret,alternative = "greater")$statistic,
                                  BDMLCI_rvmgd=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMLCI_rvmgd,subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMLCI_rvmgd,alternative = "greater")$statistic,
                                  BDMLCI_max=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMLCIxret_max,subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMLCIxret_max,alternative = "greater")$statistic,
                                  BDMXLCI_xret=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMXLCI_logxret,subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMXLCI_logxret,alternative = "greater")$statistic,
                                  BDMXLCI_rvmgd=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMXLCI_rvmgd,subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMXLCI_rvmgd,alternative = "greater")$statistic,
                                  BDMXLCI_max=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$BDMXLCIxret_max,subset(BDMI_ps,fng_classification=="Extreme Fear")$BDMXLCIxret_max,alternative = "greater")$statistic,
                                  ps_xret=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$ps_logxret,subset(BDMI_ps,fng_classification=="Extreme Fear")$ps_logxret,alternative = "greater")$statistic,
                                  ps_rvmgd=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$ps_rvmgd,subset(BDMI_ps,fng_classification=="Extreme Fear")$ps_rvmgd,alternative = "greater")$statistic,
                                  ps_max=t.test(subset(BDMI_ps,fng_classification=="Extreme Greed")$psxret_max,subset(BDMI_ps,fng_classification=="Extreme Fear")$psxret_max,alternative = "greater")$statistic))

#exclude covid
fgisort_pre<-data.frame()
for (q in c("Extreme Fear","Fear","Neutral","Greed","Extreme Greed")) {
  ss<-subset(BDMI_pre,fng_classification==q)
  fgisort_pre<-rbind(fgisort_pre,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),BDMI_max=mean(ss$BDMIxret_max),
                                            BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),BDMLCI_max=mean(ss$BDMLCIxret_max),
                                            BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),BDMXLCI_max=mean(ss$BDMXLCIxret_max),
                                            ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd),ps_max=mean(ss$psxret_max)))
}
rm(ss,q)
fgisort_pre<-rbind(fgisort_pre,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMI_logxret-subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMI_logxret)$statistic,
                                          BDMI_rvmgd=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMI_rvmgd-subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMI_rvmgd)$statistic,
                                          BDMI_max=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMIxret_max-subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMIxret_max)$statistic,
                                          BDMLCI_xret=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMLCI_logxret-subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMLCI_logxret)$statistic,
                                          BDMLCI_rvmgd=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMLCI_rvmgd-subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMLCI_rvmgd)$statistic,
                                          BDMLCI_max=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMLCIxret_max-subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMLCIxret_max)$statistic,
                                          BDMXLCI_xret=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMXLCI_logxret-subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMXLCI_logxret)$statistic,
                                          BDMXLCI_rvmgd=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMXLCI_rvmgd-subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMXLCI_rvmgd)$statistic,
                                          BDMXLCI_max=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMXLCIxret_max-subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMXLCIxret_max)$statistic,
                                          ps_xret=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$ps_logxret-subset(BDMI_pre,fng_classification=="Extreme Fear")$ps_logxret)$statistic,
                                          ps_rvmgd=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$ps_rvmgd-subset(BDMI_pre,fng_classification=="Extreme Fear")$ps_rvmgd)$statistic,
                                          ps_max=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$psxret_max-subset(BDMI_pre,fng_classification=="Extreme Fear")$psxret_max)$statistic))

fgisort_pre<-rbind(fgisort_pre,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMI_logxret,subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMI_logxret,alternative = "greater")$statistic,
                                          BDMI_rvmgd=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMI_rvmgd,subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMI_rvmgd,alternative = "greater")$statistic,
                                          BDMI_max=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMIxret_max,subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMIxret_max,alternative = "greater")$statistic,
                                          BDMLCI_xret=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMLCI_logxret,subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMLCI_logxret,alternative = "greater")$statistic,
                                          BDMLCI_rvmgd=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMLCI_rvmgd,subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMLCI_rvmgd,alternative = "greater")$statistic,
                                          BDMLCI_max=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMLCIxret_max,subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMLCIxret_max,alternative = "greater")$statistic,
                                          BDMXLCI_xret=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMXLCI_logxret,subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMXLCI_logxret,alternative = "greater")$statistic,
                                          BDMXLCI_rvmgd=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMXLCI_rvmgd,subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMXLCI_rvmgd,alternative = "greater")$statistic,
                                          BDMXLCI_max=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$BDMXLCIxret_max,subset(BDMI_pre,fng_classification=="Extreme Fear")$BDMXLCIxret_max,alternative = "greater")$statistic,
                                          ps_xret=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$ps_logxret,subset(BDMI_pre,fng_classification=="Extreme Fear")$ps_logxret,alternative = "greater")$statistic,
                                          ps_rvmgd=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$ps_rvmgd,subset(BDMI_pre,fng_classification=="Extreme Fear")$ps_rvmgd,alternative = "greater")$statistic,
                                          ps_max=t.test(subset(BDMI_pre,fng_classification=="Extreme Greed")$psxret_max,subset(BDMI_pre,fng_classification=="Extreme Fear")$psxret_max,alternative = "greater")$statistic))

#covid
fgisort_post<-data.frame()
for (q in c("Extreme Fear","Fear","Neutral","Greed","Extreme Greed")) {
  ss<-subset(BDMI_post,fng_classification==q)
  fgisort_post<-rbind(fgisort_post,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),BDMI_max=mean(ss$BDMIxret_max),
                                              BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),BDMLCI_max=mean(ss$BDMLCIxret_max),
                                              BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),BDMXLCI_max=mean(ss$BDMXLCIxret_max),
                                              ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd),ps_max=mean(ss$psxret_max)))
}
rm(ss,q)
fgisort_post<-rbind(fgisort_post,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMI_logxret-subset(BDMI_post,fng_classification=="Extreme Fear")$BDMI_logxret)$statistic,
                                            BDMI_rvmgd=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMI_rvmgd-subset(BDMI_post,fng_classification=="Extreme Fear")$BDMI_rvmgd)$statistic,
                                            BDMI_max=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMIxret_max-subset(BDMI_post,fng_classification=="Extreme Fear")$BDMIxret_max)$statistic,
                                            BDMLCI_xret=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMLCI_logxret-subset(BDMI_post,fng_classification=="Extreme Fear")$BDMLCI_logxret)$statistic,
                                            BDMLCI_rvmgd=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMLCI_rvmgd-subset(BDMI_post,fng_classification=="Extreme Fear")$BDMLCI_rvmgd)$statistic,
                                            BDMLCI_max=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMLCIxret_max-subset(BDMI_post,fng_classification=="Extreme Fear")$BDMLCIxret_max)$statistic,
                                            BDMXLCI_xret=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMXLCI_logxret-subset(BDMI_post,fng_classification=="Extreme Fear")$BDMXLCI_logxret)$statistic,
                                            BDMXLCI_rvmgd=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMXLCI_rvmgd-subset(BDMI_post,fng_classification=="Extreme Fear")$BDMXLCI_rvmgd)$statistic,
                                            BDMXLCI_max=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMXLCIxret_max-subset(BDMI_post,fng_classification=="Extreme Fear")$BDMXLCIxret_max)$statistic,
                                            ps_xret=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$ps_logxret-subset(BDMI_post,fng_classification=="Extreme Fear")$ps_logxret)$statistic,
                                            ps_rvmgd=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$ps_rvmgd-subset(BDMI_post,fng_classification=="Extreme Fear")$ps_rvmgd)$statistic,
                                            ps_max=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$psxret_max-subset(BDMI_post,fng_classification=="Extreme Fear")$psxret_max)$statistic))

fgisort_post<-rbind(fgisort_post,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMI_logxret,subset(BDMI_post,fng_classification=="Extreme Fear")$BDMI_logxret,alternative = "greater")$statistic,
                                            BDMI_rvmgd=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMI_rvmgd,subset(BDMI_post,fng_classification=="Extreme Fear")$BDMI_rvmgd,alternative = "greater")$statistic,
                                            BDMI_max=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMIxret_max,subset(BDMI_post,fng_classification=="Extreme Fear")$BDMIxret_max,alternative = "greater")$statistic,
                                            BDMLCI_xret=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMLCI_logxret,subset(BDMI_post,fng_classification=="Extreme Fear")$BDMLCI_logxret,alternative = "greater")$statistic,
                                            BDMLCI_rvmgd=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMLCI_rvmgd,subset(BDMI_post,fng_classification=="Extreme Fear")$BDMLCI_rvmgd,alternative = "greater")$statistic,
                                            BDMLCI_max=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMLCIxret_max,subset(BDMI_post,fng_classification=="Extreme Fear")$BDMLCIxret_max,alternative = "greater")$statistic,
                                            BDMXLCI_xret=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMXLCI_logxret,subset(BDMI_post,fng_classification=="Extreme Fear")$BDMXLCI_logxret,alternative = "greater")$statistic,
                                            BDMXLCI_rvmgd=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMXLCI_rvmgd,subset(BDMI_post,fng_classification=="Extreme Fear")$BDMXLCI_rvmgd,alternative = "greater")$statistic,
                                            BDMXLCI_max=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$BDMXLCIxret_max,subset(BDMI_post,fng_classification=="Extreme Fear")$BDMXLCIxret_max,alternative = "greater")$statistic,
                                            ps_xret=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$ps_logxret,subset(BDMI_post,fng_classification=="Extreme Fear")$ps_logxret,alternative = "greater")$statistic,
                                            ps_rvmgd=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$ps_rvmgd,subset(BDMI_post,fng_classification=="Extreme Fear")$ps_rvmgd,alternative = "greater")$statistic,
                                            ps_max=t.test(subset(BDMI_post,fng_classification=="Extreme Greed")$psxret_max,subset(BDMI_post,fng_classification=="Extreme Fear")$psxret_max,alternative = "greater")$statistic))



fgiregresult <- data.frame()
for (fgi in c("Extreme Fear","Fear","Neutral","Greed","Extreme Greed")) {
  ss <- subset(BDMI_ps, fng_classification == fgi)
  regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
  regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
  regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
  regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
  fgiregresult <- rbind(fgiregresult, data.frame(q = fgi,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(BDMI_ps$BDMI_rvmgd ~ BDMI_ps$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_ps$BDMLCI_rvmgd ~ BDMI_ps$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_ps$BDMXLCI_rvmgd ~ BDMI_ps$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_ps$ps_rvmgd ~ BDMI_ps$ps_logxret)
fgiregresult <- rbind(fgiregresult, data.frame(q ="fs",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(fgi,ss,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#exclude covid
for (fgi in c("Extreme Fear","Fear","Neutral","Greed","Extreme Greed")) {
  ss <- subset(BDMI_ps, fng_classification == fgi & month<202001|month>202112)
  regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
  regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
  regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
  regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
  fgiregresult <- rbind(fgiregresult, data.frame(q = fgi,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(subset(BDMI_ps,month<202001|month>202112)$BDMI_rvmgd ~ subset(BDMI_ps,month<202001|month>202112)$BDMI_logxret)
regresult_bdmlci <- lm(subset(BDMI_ps,month<202001|month>202112)$BDMLCI_rvmgd ~ subset(BDMI_ps,month<202001|month>202112)$BDMLCI_logxret)
regresult_bdmxlci <- lm(subset(BDMI_ps,month<202001|month>202112)$BDMXLCI_rvmgd ~ subset(BDMI_ps,month<202001|month>202112)$BDMXLCI_logxret)
regresult_ps <- lm(subset(BDMI_ps,month<202001|month>202112)$ps_rvmgd ~ subset(BDMI_ps,month<202001|month>202112)$ps_logxret)
fgiregresult <- rbind(fgiregresult, data.frame(q ="fs_pre",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(fgi,ss,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#covid
for (fgi in c("Extreme Fear","Fear","Neutral","Greed","Extreme Greed")) {
  ss <- subset(BDMI_ps, fng_classification == fgi & month>=202001&month<=202112)
  regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
  regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
  regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
  regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
  fgiregresult <- rbind(fgiregresult, data.frame(q = fgi,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(subset(BDMI_ps,month>=202001&month<=202112)$BDMI_rvmgd ~ subset(BDMI_ps,month>=202001&month<=202112)$BDMI_logxret)
regresult_bdmlci <- lm(subset(BDMI_ps,month>=202001&month<=202112)$BDMLCI_rvmgd ~ subset(BDMI_ps,month>=202001&month<=202112)$BDMLCI_logxret)
regresult_bdmxlci <- lm(subset(BDMI_ps,month>=202001&month<=202112)$BDMXLCI_rvmgd ~ subset(BDMI_ps,month>=202001&month<=202112)$BDMXLCI_logxret)
regresult_ps <- lm(subset(BDMI_ps,month>=202001&month<=202112)$ps_rvmgd ~ subset(BDMI_ps,month>=202001&month<=202112)$ps_logxret)
fgiregresult <- rbind(fgiregresult, data.frame(q ="fs_post",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))









#max regression
maxregresult <- data.frame()
for (qmax in 1:4) {
  regresult_bdmi <- lm(subset(BDMI_ps, BDMI_maxsort == qmax)$BDMI_rvmgd ~ subset(BDMI_ps, BDMI_maxsort == qmax)$BDMI_logxret)
  regresult_bdmlci <- lm(subset(BDMI_ps, BDMLCI_maxsort == qmax)$BDMLCI_rvmgd ~ subset(BDMI_ps, BDMLCI_maxsort == qmax)$BDMLCI_logxret)
  regresult_bdmxlci <- lm(subset(BDMI_ps, BDMXLCI_maxsort == qmax)$BDMXLCI_rvmgd ~ subset(BDMI_ps, BDMXLCI_maxsort == qmax)$BDMXLCI_logxret)
  regresult_ps <- lm(subset(BDMI_ps, ps_maxsort == qmax)$ps_rvmgd ~ subset(BDMI_ps, ps_maxsort == qmax)$ps_logxret)
  maxregresult <- rbind(maxregresult, data.frame(q = qmax,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(BDMI_ps$BDMI_rvmgd ~ BDMI_ps$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_ps$BDMLCI_rvmgd ~ BDMI_ps$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_ps$BDMXLCI_rvmgd ~ BDMI_ps$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_ps$ps_rvmgd ~ BDMI_ps$ps_logxret)
maxregresult <- rbind(maxregresult, data.frame(q ="fs",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(qmax,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#pre-covid
for (qmax in 1:4) {
  regresult_bdmi <- lm(subset(BDMI_pre, BDMI_maxsort == qmax)$BDMI_rvmgd ~ subset(BDMI_pre, BDMI_maxsort == qmax)$BDMI_logxret)
  regresult_bdmlci <- lm(subset(BDMI_pre, BDMLCI_maxsort == qmax)$BDMLCI_rvmgd ~ subset(BDMI_pre, BDMLCI_maxsort == qmax)$BDMLCI_logxret)
  regresult_bdmxlci <- lm(subset(BDMI_pre, BDMXLCI_maxsort == qmax)$BDMXLCI_rvmgd ~ subset(BDMI_pre, BDMXLCI_maxsort == qmax)$BDMXLCI_logxret)
  regresult_ps <- lm(subset(BDMI_pre, ps_maxsort == qmax)$ps_rvmgd ~ subset(BDMI_pre, ps_maxsort == qmax)$ps_logxret)
  maxregresult <- rbind(maxregresult, data.frame(q = qmax,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(BDMI_pre$BDMI_rvmgd ~ BDMI_pre$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_pre$BDMLCI_rvmgd ~ BDMI_pre$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_pre$BDMXLCI_rvmgd ~ BDMI_pre$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_pre$ps_rvmgd ~ BDMI_pre$ps_logxret)
maxregresult <- rbind(maxregresult, data.frame(q ="fs_pre",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(qmax,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#covid
for (qmax in 1:4) {
  regresult_bdmi <- lm(subset(BDMI_post, BDMI_maxsort == qmax)$BDMI_rvmgd ~ subset(BDMI_post, BDMI_maxsort == qmax)$BDMI_logxret)
  regresult_bdmlci <- lm(subset(BDMI_post, BDMLCI_maxsort == qmax)$BDMLCI_rvmgd ~ subset(BDMI_post, BDMLCI_maxsort == qmax)$BDMLCI_logxret)
  regresult_bdmxlci <- lm(subset(BDMI_post, BDMXLCI_maxsort == qmax)$BDMXLCI_rvmgd ~ subset(BDMI_post, BDMXLCI_maxsort == qmax)$BDMXLCI_logxret)
  regresult_ps <- lm(subset(BDMI_post, ps_maxsort == qmax)$ps_rvmgd ~ subset(BDMI_post, ps_maxsort == qmax)$ps_logxret)
  maxregresult <- rbind(maxregresult, data.frame(q = qmax,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(BDMI_post$BDMI_rvmgd ~ BDMI_post$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_post$BDMLCI_rvmgd ~ BDMI_post$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_post$BDMXLCI_rvmgd ~ BDMI_post$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_post$ps_rvmgd ~ BDMI_post$ps_logxret)
maxregresult <- rbind(maxregresult, data.frame(q ="fs_post",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))











#sgi sort
sgiregresult <- data.frame()
for (sgi in c("Shrinking","Intermediate","Growth")) {
  ss <- subset(BDMI_ps, phase == sgi)
  regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
  regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
  regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
  regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
  sgiregresult <- rbind(sgiregresult, data.frame(q = sgi,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(BDMI_ps$BDMI_rvmgd ~ BDMI_ps$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_ps$BDMLCI_rvmgd ~ BDMI_ps$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_ps$BDMXLCI_rvmgd ~ BDMI_ps$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_ps$ps_rvmgd ~ BDMI_ps$ps_logxret)
sgiregresult <- rbind(sgiregresult, data.frame(q ="fs",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(sgi,ss,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#exclude covid
for (sgi in c("Shrinking","Intermediate","Growth")) {
  ss <- subset(BDMI_ps, phase == sgi & month<202001|month>202112)
  regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
  regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
  regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
  regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
  sgiregresult <- rbind(sgiregresult, data.frame(q = sgi,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(subset(BDMI_ps,month<202001|month>202112)$BDMI_rvmgd ~ subset(BDMI_ps,month<202001|month>202112)$BDMI_logxret)
regresult_bdmlci <- lm(subset(BDMI_ps,month<202001|month>202112)$BDMLCI_rvmgd ~ subset(BDMI_ps,month<202001|month>202112)$BDMLCI_logxret)
regresult_bdmxlci <- lm(subset(BDMI_ps,month<202001|month>202112)$BDMXLCI_rvmgd ~ subset(BDMI_ps,month<202001|month>202112)$BDMXLCI_logxret)
regresult_ps <- lm(subset(BDMI_ps,month<202001|month>202112)$ps_rvmgd ~ subset(BDMI_ps,month<202001|month>202112)$ps_logxret)
sgiregresult <- rbind(sgiregresult, data.frame(q ="fs_pre",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(sgi,ss,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#covid
for (sgi in c("Shrinking","Intermediate","Growth")) {
  ss <- subset(BDMI_ps, phase == sgi & month>=202001&month<=202112)
  regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
  regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
  regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
  regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
  sgiregresult <- rbind(sgiregresult, data.frame(q = sgi,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(subset(BDMI_ps,month>=202001&month<=202112)$BDMI_rvmgd ~ subset(BDMI_ps,month>=202001&month<=202112)$BDMI_logxret)
regresult_bdmlci <- lm(subset(BDMI_ps,month>=202001&month<=202112)$BDMLCI_rvmgd ~ subset(BDMI_ps,month>=202001&month<=202112)$BDMLCI_logxret)
regresult_bdmxlci <- lm(subset(BDMI_ps,month>=202001&month<=202112)$BDMXLCI_rvmgd ~ subset(BDMI_ps,month>=202001&month<=202112)$BDMXLCI_logxret)
regresult_ps <- lm(subset(BDMI_ps,month>=202001&month<=202112)$ps_rvmgd ~ subset(BDMI_ps,month>=202001&month<=202112)$ps_logxret)
sgiregresult <- rbind(sgiregresult, data.frame(q ="fs_post",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(sgi,ss,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)








sgisort<-data.frame()
for (q in c("Shrinking","Intermediate","Growth")) {
  ss<-subset(BDMI_ps,phase==q)
  sgisort<-rbind(sgisort,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),BDMI_max=mean(ss$BDMIxret_max),
                                    BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),BDMLCI_max=mean(ss$BDMLCIxret_max),
                                    BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),BDMXLCI_max=mean(ss$BDMXLCIxret_max),
                                    ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd),ps_max=mean(ss$psxret_max)))
}
rm(ss,q)
sgisort<-rbind(sgisort,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_ps,phase=="Growth")$BDMI_logxret-subset(BDMI_ps,phase=="Shrinking")$BDMI_logxret)$statistic,
                                  BDMI_rvmgd=t.test(subset(BDMI_ps,phase=="Growth")$BDMI_rvmgd-subset(BDMI_ps,phase=="Shrinking")$BDMI_rvmgd)$statistic,
                                  BDMI_max=t.test(subset(BDMI_ps,phase=="Growth")$BDMIxret_max-subset(BDMI_ps,phase=="Shrinking")$BDMIxret_max)$statistic,
                                  BDMLCI_xret=t.test(subset(BDMI_ps,phase=="Growth")$BDMLCI_logxret-subset(BDMI_ps,phase=="Shrinking")$BDMLCI_logxret)$statistic,
                                  BDMLCI_rvmgd=t.test(subset(BDMI_ps,phase=="Growth")$BDMLCI_rvmgd-subset(BDMI_ps,phase=="Shrinking")$BDMLCI_rvmgd)$statistic,
                                  BDMLCI_max=t.test(subset(BDMI_ps,phase=="Growth")$BDMLCIxret_max-subset(BDMI_ps,phase=="Shrinking")$BDMLCIxret_max)$statistic,
                                  BDMXLCI_xret=t.test(subset(BDMI_ps,phase=="Growth")$BDMXLCI_logxret-subset(BDMI_ps,phase=="Shrinking")$BDMXLCI_logxret)$statistic,
                                  BDMXLCI_rvmgd=t.test(subset(BDMI_ps,phase=="Growth")$BDMXLCI_rvmgd-subset(BDMI_ps,phase=="Shrinking")$BDMXLCI_rvmgd)$statistic,
                                  BDMXLCI_max=t.test(subset(BDMI_ps,phase=="Growth")$BDMXLCIxret_max-subset(BDMI_ps,phase=="Shrinking")$BDMXLCIxret_max)$statistic,
                                  ps_xret=t.test(subset(BDMI_ps,phase=="Growth")$ps_logxret-subset(BDMI_ps,phase=="Shrinking")$ps_logxret)$statistic,
                                  ps_rvmgd=t.test(subset(BDMI_ps,phase=="Growth")$ps_rvmgd-subset(BDMI_ps,phase=="Shrinking")$ps_rvmgd)$statistic,
                                  ps_max=t.test(subset(BDMI_ps,phase=="Growth")$psxret_max-subset(BDMI_ps,phase=="Shrinking")$psxret_max)$statistic))

sgisort<-rbind(sgisort,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_ps,phase=="Growth")$BDMI_logxret,subset(BDMI_ps,phase=="Shrinking")$BDMI_logxret,alternative = "greater")$statistic,
                                  BDMI_rvmgd=t.test(subset(BDMI_ps,phase=="Growth")$BDMI_rvmgd,subset(BDMI_ps,phase=="Shrinking")$BDMI_rvmgd,alternative = "greater")$statistic,
                                  BDMI_max=t.test(subset(BDMI_ps,phase=="Growth")$BDMIxret_max,subset(BDMI_ps,phase=="Shrinking")$BDMIxret_max,alternative = "greater")$statistic,
                                  BDMLCI_xret=t.test(subset(BDMI_ps,phase=="Growth")$BDMLCI_logxret,subset(BDMI_ps,phase=="Shrinking")$BDMLCI_logxret,alternative = "greater")$statistic,
                                  BDMLCI_rvmgd=t.test(subset(BDMI_ps,phase=="Growth")$BDMLCI_rvmgd,subset(BDMI_ps,phase=="Shrinking")$BDMLCI_rvmgd,alternative = "greater")$statistic,
                                  BDMLCI_max=t.test(subset(BDMI_ps,phase=="Growth")$BDMLCIxret_max,subset(BDMI_ps,phase=="Shrinking")$BDMLCIxret_max,alternative = "greater")$statistic,
                                  BDMXLCI_xret=t.test(subset(BDMI_ps,phase=="Growth")$BDMXLCI_logxret,subset(BDMI_ps,phase=="Shrinking")$BDMXLCI_logxret,alternative = "greater")$statistic,
                                  BDMXLCI_rvmgd=t.test(subset(BDMI_ps,phase=="Growth")$BDMXLCI_rvmgd,subset(BDMI_ps,phase=="Shrinking")$BDMXLCI_rvmgd,alternative = "greater")$statistic,
                                  BDMXLCI_max=t.test(subset(BDMI_ps,phase=="Growth")$BDMXLCIxret_max,subset(BDMI_ps,phase=="Shrinking")$BDMXLCIxret_max,alternative = "greater")$statistic,
                                  ps_xret=t.test(subset(BDMI_ps,phase=="Growth")$ps_logxret,subset(BDMI_ps,phase=="Shrinking")$ps_logxret,alternative = "greater")$statistic,
                                  ps_rvmgd=t.test(subset(BDMI_ps,phase=="Growth")$ps_rvmgd,subset(BDMI_ps,phase=="Shrinking")$ps_rvmgd,alternative = "greater")$statistic,
                                  ps_max=t.test(subset(BDMI_ps,phase=="Growth")$psxret_max,subset(BDMI_ps,phase=="Shrinking")$psxret_max,alternative = "greater")$statistic))

#exclude covid
sgisort_pre<-data.frame()
for (q in c("Shrinking","Intermediate","Growth")) {
  ss<-subset(BDMI_pre,phase==q)
  sgisort_pre<-rbind(sgisort_pre,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),BDMI_max=mean(ss$BDMIxret_max),
                                            BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),BDMLCI_max=mean(ss$BDMLCIxret_max),
                                            BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),BDMXLCI_max=mean(ss$BDMXLCIxret_max),
                                            ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd),ps_max=mean(ss$psxret_max)))
}
rm(ss,q)
sgisort_pre<-rbind(sgisort_pre,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_pre,phase=="Growth")$BDMI_logxret-subset(BDMI_pre,phase=="Shrinking")$BDMI_logxret)$statistic,
                                          BDMI_rvmgd=t.test(subset(BDMI_pre,phase=="Growth")$BDMI_rvmgd-subset(BDMI_pre,phase=="Shrinking")$BDMI_rvmgd)$statistic,
                                          BDMI_max=t.test(subset(BDMI_pre,phase=="Growth")$BDMIxret_max-subset(BDMI_pre,phase=="Shrinking")$BDMIxret_max)$statistic,
                                          BDMLCI_xret=t.test(subset(BDMI_pre,phase=="Growth")$BDMLCI_logxret-subset(BDMI_pre,phase=="Shrinking")$BDMLCI_logxret)$statistic,
                                          BDMLCI_rvmgd=t.test(subset(BDMI_pre,phase=="Growth")$BDMLCI_rvmgd-subset(BDMI_pre,phase=="Shrinking")$BDMLCI_rvmgd)$statistic,
                                          BDMLCI_max=t.test(subset(BDMI_pre,phase=="Growth")$BDMLCIxret_max-subset(BDMI_pre,phase=="Shrinking")$BDMLCIxret_max)$statistic,
                                          BDMXLCI_xret=t.test(subset(BDMI_pre,phase=="Growth")$BDMXLCI_logxret-subset(BDMI_pre,phase=="Shrinking")$BDMXLCI_logxret)$statistic,
                                          BDMXLCI_rvmgd=t.test(subset(BDMI_pre,phase=="Growth")$BDMXLCI_rvmgd-subset(BDMI_pre,phase=="Shrinking")$BDMXLCI_rvmgd)$statistic,
                                          BDMXLCI_max=t.test(subset(BDMI_pre,phase=="Growth")$BDMXLCIxret_max-subset(BDMI_pre,phase=="Shrinking")$BDMXLCIxret_max)$statistic,
                                          ps_xret=t.test(subset(BDMI_pre,phase=="Growth")$ps_logxret-subset(BDMI_pre,phase=="Shrinking")$ps_logxret)$statistic,
                                          ps_rvmgd=t.test(subset(BDMI_pre,phase=="Growth")$ps_rvmgd-subset(BDMI_pre,phase=="Shrinking")$ps_rvmgd)$statistic,
                                          ps_max=t.test(subset(BDMI_pre,phase=="Growth")$psxret_max-subset(BDMI_pre,phase=="Shrinking")$psxret_max)$statistic))

sgisort_pre<-rbind(sgisort_pre,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_pre,phase=="Growth")$BDMI_logxret,subset(BDMI_pre,phase=="Shrinking")$BDMI_logxret,alternative = "greater")$statistic,
                                          BDMI_rvmgd=t.test(subset(BDMI_pre,phase=="Growth")$BDMI_rvmgd,subset(BDMI_pre,phase=="Shrinking")$BDMI_rvmgd,alternative = "greater")$statistic,
                                          BDMI_max=t.test(subset(BDMI_pre,phase=="Growth")$BDMIxret_max,subset(BDMI_pre,phase=="Shrinking")$BDMIxret_max,alternative = "greater")$statistic,
                                          BDMLCI_xret=t.test(subset(BDMI_pre,phase=="Growth")$BDMLCI_logxret,subset(BDMI_pre,phase=="Shrinking")$BDMLCI_logxret,alternative = "greater")$statistic,
                                          BDMLCI_rvmgd=t.test(subset(BDMI_pre,phase=="Growth")$BDMLCI_rvmgd,subset(BDMI_pre,phase=="Shrinking")$BDMLCI_rvmgd,alternative = "greater")$statistic,
                                          BDMLCI_max=t.test(subset(BDMI_pre,phase=="Growth")$BDMLCIxret_max,subset(BDMI_pre,phase=="Shrinking")$BDMLCIxret_max,alternative = "greater")$statistic,
                                          BDMXLCI_xret=t.test(subset(BDMI_pre,phase=="Growth")$BDMXLCI_logxret,subset(BDMI_pre,phase=="Shrinking")$BDMXLCI_logxret,alternative = "greater")$statistic,
                                          BDMXLCI_rvmgd=t.test(subset(BDMI_pre,phase=="Growth")$BDMXLCI_rvmgd,subset(BDMI_pre,phase=="Shrinking")$BDMXLCI_rvmgd,alternative = "greater")$statistic,
                                          BDMXLCI_max=t.test(subset(BDMI_pre,phase=="Growth")$BDMXLCIxret_max,subset(BDMI_pre,phase=="Shrinking")$BDMXLCIxret_max,alternative = "greater")$statistic,
                                          ps_xret=t.test(subset(BDMI_pre,phase=="Growth")$ps_logxret,subset(BDMI_pre,phase=="Shrinking")$ps_logxret,alternative = "greater")$statistic,
                                          ps_rvmgd=t.test(subset(BDMI_pre,phase=="Growth")$ps_rvmgd,subset(BDMI_pre,phase=="Shrinking")$ps_rvmgd,alternative = "greater")$statistic,
                                          ps_max=t.test(subset(BDMI_pre,phase=="Growth")$psxret_max,subset(BDMI_pre,phase=="Shrinking")$psxret_max,alternative = "greater")$statistic))

#covid
sgisort_post<-data.frame()
for (q in c("Shrinking","Intermediate","Growth")) {
  ss<-subset(BDMI_post,phase==q)
  sgisort_post<-rbind(sgisort_post,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),BDMI_max=mean(ss$BDMIxret_max),
                                              BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),BDMLCI_max=mean(ss$BDMLCIxret_max),
                                              BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),BDMXLCI_max=mean(ss$BDMXLCIxret_max),
                                              ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd),ps_max=mean(ss$psxret_max)))
}
rm(ss,q)
sgisort_post<-rbind(sgisort_post,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_post,phase=="Growth")$BDMI_logxret-subset(BDMI_post,phase=="Shrinking")$BDMI_logxret)$statistic,
                                            BDMI_rvmgd=t.test(subset(BDMI_post,phase=="Growth")$BDMI_rvmgd-subset(BDMI_post,phase=="Shrinking")$BDMI_rvmgd)$statistic,
                                            BDMI_max=t.test(subset(BDMI_post,phase=="Growth")$BDMIxret_max-subset(BDMI_post,phase=="Shrinking")$BDMIxret_max)$statistic,
                                            BDMLCI_xret=t.test(subset(BDMI_post,phase=="Growth")$BDMLCI_logxret-subset(BDMI_post,phase=="Shrinking")$BDMLCI_logxret)$statistic,
                                            BDMLCI_rvmgd=t.test(subset(BDMI_post,phase=="Growth")$BDMLCI_rvmgd-subset(BDMI_post,phase=="Shrinking")$BDMLCI_rvmgd)$statistic,
                                            BDMLCI_max=t.test(subset(BDMI_post,phase=="Growth")$BDMLCIxret_max-subset(BDMI_post,phase=="Shrinking")$BDMLCIxret_max)$statistic,
                                            BDMXLCI_xret=t.test(subset(BDMI_post,phase=="Growth")$BDMXLCI_logxret-subset(BDMI_post,phase=="Shrinking")$BDMXLCI_logxret)$statistic,
                                            BDMXLCI_rvmgd=t.test(subset(BDMI_post,phase=="Growth")$BDMXLCI_rvmgd-subset(BDMI_post,phase=="Shrinking")$BDMXLCI_rvmgd)$statistic,
                                            BDMXLCI_max=t.test(subset(BDMI_post,phase=="Growth")$BDMXLCIxret_max-subset(BDMI_post,phase=="Shrinking")$BDMXLCIxret_max)$statistic,
                                            ps_xret=t.test(subset(BDMI_post,phase=="Growth")$ps_logxret-subset(BDMI_post,phase=="Shrinking")$ps_logxret)$statistic,
                                            ps_rvmgd=t.test(subset(BDMI_post,phase=="Growth")$ps_rvmgd-subset(BDMI_post,phase=="Shrinking")$ps_rvmgd)$statistic,
                                            ps_max=t.test(subset(BDMI_post,phase=="Growth")$psxret_max-subset(BDMI_post,phase=="Shrinking")$psxret_max)$statistic))

sgisort_post<-rbind(sgisort_post,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_post,phase=="Growth")$BDMI_logxret,subset(BDMI_post,phase=="Shrinking")$BDMI_logxret,alternative = "greater")$statistic,
                                            BDMI_rvmgd=t.test(subset(BDMI_post,phase=="Growth")$BDMI_rvmgd,subset(BDMI_post,phase=="Shrinking")$BDMI_rvmgd,alternative = "greater")$statistic,
                                            BDMI_max=t.test(subset(BDMI_post,phase=="Growth")$BDMIxret_max,subset(BDMI_post,phase=="Shrinking")$BDMIxret_max,alternative = "greater")$statistic,
                                            BDMLCI_xret=t.test(subset(BDMI_post,phase=="Growth")$BDMLCI_logxret,subset(BDMI_post,phase=="Shrinking")$BDMLCI_logxret,alternative = "greater")$statistic,
                                            BDMLCI_rvmgd=t.test(subset(BDMI_post,phase=="Growth")$BDMLCI_rvmgd,subset(BDMI_post,phase=="Shrinking")$BDMLCI_rvmgd,alternative = "greater")$statistic,
                                            BDMLCI_max=t.test(subset(BDMI_post,phase=="Growth")$BDMLCIxret_max,subset(BDMI_post,phase=="Shrinking")$BDMLCIxret_max,alternative = "greater")$statistic,
                                            BDMXLCI_xret=t.test(subset(BDMI_post,phase=="Growth")$BDMXLCI_logxret,subset(BDMI_post,phase=="Shrinking")$BDMXLCI_logxret,alternative = "greater")$statistic,
                                            BDMXLCI_rvmgd=t.test(subset(BDMI_post,phase=="Growth")$BDMXLCI_rvmgd,subset(BDMI_post,phase=="Shrinking")$BDMXLCI_rvmgd,alternative = "greater")$statistic,
                                            BDMXLCI_max=t.test(subset(BDMI_post,phase=="Growth")$BDMXLCIxret_max,subset(BDMI_post,phase=="Shrinking")$BDMXLCIxret_max,alternative = "greater")$statistic,
                                            ps_xret=t.test(subset(BDMI_post,phase=="Growth")$ps_logxret,subset(BDMI_post,phase=="Shrinking")$ps_logxret,alternative = "greater")$statistic,
                                            ps_rvmgd=t.test(subset(BDMI_post,phase=="Growth")$ps_rvmgd,subset(BDMI_post,phase=="Shrinking")$ps_rvmgd,alternative = "greater")$statistic,
                                            ps_max=t.test(subset(BDMI_post,phase=="Growth")$psxret_max,subset(BDMI_post,phase=="Shrinking")$psxret_max,alternative = "greater")$statistic))


sgiregresult <- data.frame()
for (sgi in c("Shrinking","Intermediate","Growth")) {
  ss <- subset(BDMI_ps, phase == sgi)
  regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
  regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
  regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
  regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
  sgiregresult <- rbind(sgiregresult, data.frame(q = sgi,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(BDMI_ps$BDMI_rvmgd ~ BDMI_ps$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_ps$BDMLCI_rvmgd ~ BDMI_ps$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_ps$BDMXLCI_rvmgd ~ BDMI_ps$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_ps$ps_rvmgd ~ BDMI_ps$ps_logxret)
sgiregresult <- rbind(sgiregresult, data.frame(q ="fs",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(sgi,ss,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#exclude covid
for (sgi in c("Shrinking","Intermediate","Growth")) {
  ss <- subset(BDMI_ps, phase == sgi & month<202001|month>202112)
  regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
  regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
  regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
  regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
  sgiregresult <- rbind(sgiregresult, data.frame(q = sgi,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(subset(BDMI_ps,month<202001|month>202112)$BDMI_rvmgd ~ subset(BDMI_ps,month<202001|month>202112)$BDMI_logxret)
regresult_bdmlci <- lm(subset(BDMI_ps,month<202001|month>202112)$BDMLCI_rvmgd ~ subset(BDMI_ps,month<202001|month>202112)$BDMLCI_logxret)
regresult_bdmxlci <- lm(subset(BDMI_ps,month<202001|month>202112)$BDMXLCI_rvmgd ~ subset(BDMI_ps,month<202001|month>202112)$BDMXLCI_logxret)
regresult_ps <- lm(subset(BDMI_ps,month<202001|month>202112)$ps_rvmgd ~ subset(BDMI_ps,month<202001|month>202112)$ps_logxret)
sgiregresult <- rbind(sgiregresult, data.frame(q ="fs_pre",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(sgi,ss,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#covid
for (sgi in c("Shrinking","Intermediate","Growth")) {
  ss <- subset(BDMI_ps, phase == sgi & month>=202001&month<=202112)
  regresult_bdmi <- lm(ss$BDMI_rvmgd ~ ss$BDMI_logxret)
  regresult_bdmlci <- lm(ss$BDMLCI_rvmgd ~ ss$BDMLCI_logxret)
  regresult_bdmxlci <- lm(ss$BDMXLCI_rvmgd ~ ss$BDMXLCI_logxret)
  regresult_ps <- lm(ss$ps_rvmgd ~ ss$ps_logxret)
  sgiregresult <- rbind(sgiregresult, data.frame(q = sgi,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(subset(BDMI_ps,month>=202001&month<=202112)$BDMI_rvmgd ~ subset(BDMI_ps,month>=202001&month<=202112)$BDMI_logxret)
regresult_bdmlci <- lm(subset(BDMI_ps,month>=202001&month<=202112)$BDMLCI_rvmgd ~ subset(BDMI_ps,month>=202001&month<=202112)$BDMLCI_logxret)
regresult_bdmxlci <- lm(subset(BDMI_ps,month>=202001&month<=202112)$BDMXLCI_rvmgd ~ subset(BDMI_ps,month>=202001&month<=202112)$BDMXLCI_logxret)
regresult_ps <- lm(subset(BDMI_ps,month>=202001&month<=202112)$ps_rvmgd ~ subset(BDMI_ps,month>=202001&month<=202112)$ps_logxret)
sgiregresult <- rbind(sgiregresult, data.frame(q ="fs_post",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))










#vix sorts
BDMI_vix$VIX_sort<-cut(BDMI_vix$VIX,breaks = quantile(BDMI_vix$VIX,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)

vixsort<-data.frame()
for (q in 1:4) {
  ss<-subset(BDMI_vix,VIX_sort==q)
  vixsort<-rbind(vixsort,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),
                                    BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),
                                    BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),
                                    ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd)))
}
rm(ss,q)

vixsort<-rbind(vixsort,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_vix, VIX_sort == 4)$BDMI_logxret,subset(BDMI_vix, VIX_sort == 1)$BDMI_logxret,alternative = "greater")$statistic,
                                  BDMI_rvmgd=t.test(subset(BDMI_vix,VIX_sort==4)$BDMI_rvmgd,subset(BDMI_vix,VIX_sort==1)$BDMI_rvmgd,alternative = "greater")$statistic,
                                  BDMLCI_xret=t.test(subset(BDMI_vix,VIX_sort==4)$BDMLCI_logxret,subset(BDMI_vix,VIX_sort==1)$BDMLCI_logxret,alternative = "greater")$statistic,
                                  BDMLCI_rvmgd=t.test(subset(BDMI_vix,VIX_sort==4)$BDMLCI_rvmgd,subset(BDMI_vix,VIX_sort==1)$BDMLCI_rvmgd,alternative = "greater")$statistic,
                                  BDMXLCI_xret=t.test(subset(BDMI_vix,VIX_sort==4)$BDMXLCI_logxret,subset(BDMI_vix,VIX_sort==1)$BDMXLCI_logxret,alternative = "greater")$statistic,
                                  BDMXLCI_rvmgd=t.test(subset(BDMI_vix,VIX_sort==4)$BDMXLCI_rvmgd,subset(BDMI_vix,VIX_sort==1)$BDMXLCI_rvmgd,alternative = "greater")$statistic,
                                  ps_xret=t.test(subset(BDMI_vix,VIX_sort==4)$ps_logxret,subset(BDMI_vix,VIX_sort==1)$ps_logxret,alternative = "greater")$statistic,
                                  ps_rvmgd=t.test(subset(BDMI_vix,VIX_sort==4)$ps_rvmgd,subset(BDMI_vix,VIX_sort==1)$ps_rvmgd,alternative = "greater")$statistic))

#separate covid
#pre covid
BDMI_pre<-subset(BDMI_vix,month<202001|month>202112)

BDMI_pre$VIX_sort<-cut(BDMI_pre$VIX,breaks = quantile(BDMI_pre$VIX,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)

vixsort_pre<-data.frame()
for (q in 1:4) {
  ss<-subset(BDMI_pre,VIX_sort==q)
  
  vixsort_pre<-rbind(vixsort_pre,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),
                                            BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),
                                            BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),
                                            ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd)))
}
rm(ss,q)

vixsort_pre<-rbind(vixsort_pre,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_pre,VIX_sort==4)$BDMI_logxret-subset(BDMI_pre,VIX_sort==1)$BDMI_logxret)$statistic,
                                          BDMI_rvmgd=t.test(subset(BDMI_pre,VIX_sort==4)$BDMI_rvmgd-subset(BDMI_pre,VIX_sort==1)$BDMI_rvmgd)$statistic,
                                          BDMLCI_xret=t.test(subset(BDMI_pre,VIX_sort==4)$BDMLCI_logxret-subset(BDMI_pre,VIX_sort==1)$BDMLCI_logxret)$statistic,
                                          BDMLCI_rvmgd=t.test(subset(BDMI_pre,VIX_sort==4)$BDMLCI_rvmgd-subset(BDMI_pre,VIX_sort==1)$BDMLCI_rvmgd)$statistic,
                                          BDMXLCI_xret=t.test(subset(BDMI_pre,VIX_sort==4)$BDMXLCI_logxret-subset(BDMI_pre,VIX_sort==1)$BDMXLCI_logxret)$statistic,
                                          BDMXLCI_rvmgd=t.test(subset(BDMI_pre,VIX_sort==4)$BDMXLCI_rvmgd-subset(BDMI_pre,VIX_sort==1)$BDMXLCI_rvmgd)$statistic,
                                          ps_xret=t.test(subset(BDMI_pre,VIX_sort==4)$ps_logxret-subset(BDMI_pre,VIX_sort==1)$ps_logxret)$statistic,
                                          ps_rvmgd=t.test(subset(BDMI_pre,VIX_sort==4)$ps_rvmgd-subset(BDMI_pre,VIX_sort==1)$ps_rvmgd)$statistic))

#covid
BDMI_post<-subset(BDMI_vix,month>=202001&month<=202112)

BDMI_post$VIX_sort<-cut(BDMI_post$VIX,breaks = quantile(BDMI_post$VIX,probs = seq(0,1,by=0.25),na.rm=T),include.lowest=TRUE,labels=FALSE)

vixsort_post<-data.frame()
for (q in 1:4) {
  ss<-subset(BDMI_post,VIX_sort==q)
  
  vixsort_post<-rbind(vixsort_post,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),
                                              BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),
                                              BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),
                                              ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd)))
}
rm(ss,q)

vixsort_post<-rbind(vixsort_post,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_post,VIX_sort==4)$BDMI_logxret-subset(BDMI_post,VIX_sort==1)$BDMI_logxret)$statistic,
                                            BDMI_rvmgd=t.test(subset(BDMI_post,VIX_sort==4)$BDMI_rvmgd-subset(BDMI_post,VIX_sort==1)$BDMI_rvmgd)$statistic,
                                            BDMLCI_xret=t.test(subset(BDMI_post,VIX_sort==4)$BDMLCI_logxret-subset(BDMI_post,VIX_sort==1)$BDMLCI_logxret)$statistic,
                                            BDMLCI_rvmgd=t.test(subset(BDMI_post,VIX_sort==4)$BDMLCI_rvmgd-subset(BDMI_post,VIX_sort==1)$BDMLCI_rvmgd)$statistic,
                                            BDMXLCI_xret=t.test(subset(BDMI_post,VIX_sort==4)$BDMXLCI_logxret-subset(BDMI_post,VIX_sort==1)$BDMXLCI_logxret)$statistic,
                                            BDMXLCI_rvmgd=t.test(subset(BDMI_post,VIX_sort==4)$BDMXLCI_rvmgd-subset(BDMI_post,VIX_sort==1)$BDMXLCI_rvmgd)$statistic,
                                            ps_xret=t.test(subset(BDMI_post,VIX_sort==4)$ps_logxret-subset(BDMI_post,VIX_sort==1)$ps_logxret)$statistic,
                                            ps_rvmgd=t.test(subset(BDMI_post,VIX_sort==4)$ps_rvmgd-subset(BDMI_post,VIX_sort==1)$ps_rvmgd)$statistic))




vixregresult <- data.frame()
for (qmax in 1:4) {
  regresult_bdmi <- lm(subset(BDMI_vix, VIX_sort == qmax)$BDMI_rvmgd ~ subset(BDMI_vix, VIX_sort == qmax)$BDMI_logxret)
  regresult_bdmlci <- lm(subset(BDMI_vix, VIX_sort == qmax)$BDMLCI_rvmgd ~ subset(BDMI_vix, VIX_sort == qmax)$BDMLCI_logxret)
  regresult_bdmxlci <- lm(subset(BDMI_vix, VIX_sort == qmax)$BDMXLCI_rvmgd ~ subset(BDMI_vix, VIX_sort == qmax)$BDMXLCI_logxret)
  regresult_ps <- lm(subset(BDMI_vix, VIX_sort == qmax)$ps_rvmgd ~ subset(BDMI_vix, VIX_sort == qmax)$ps_logxret)
  vixregresult <- rbind(vixregresult, data.frame(q = qmax,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(BDMI_vix$BDMI_rvmgd ~ BDMI_vix$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_vix$BDMLCI_rvmgd ~ BDMI_vix$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_vix$BDMXLCI_rvmgd ~ BDMI_vix$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_vix$ps_rvmgd ~ BDMI_vix$ps_logxret)
vixregresult <- rbind(vixregresult, data.frame(q ="fs",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(qmax,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#pre-covid
for (qmax in 1:4) {
  regresult_bdmi <- lm(subset(BDMI_pre, VIX_sort == qmax)$BDMI_rvmgd ~ subset(BDMI_pre, VIX_sort == qmax)$BDMI_logxret)
  regresult_bdmlci <- lm(subset(BDMI_pre, VIX_sort == qmax)$BDMLCI_rvmgd ~ subset(BDMI_pre, VIX_sort == qmax)$BDMLCI_logxret)
  regresult_bdmxlci <- lm(subset(BDMI_pre, VIX_sort == qmax)$BDMXLCI_rvmgd ~ subset(BDMI_pre, VIX_sort == qmax)$BDMXLCI_logxret)
  regresult_ps <- lm(subset(BDMI_pre, VIX_sort == qmax)$ps_rvmgd ~ subset(BDMI_pre, VIX_sort == qmax)$ps_logxret)
  vixregresult <- rbind(vixregresult, data.frame(q = qmax,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(BDMI_pre$BDMI_rvmgd ~ BDMI_pre$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_pre$BDMLCI_rvmgd ~ BDMI_pre$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_pre$BDMXLCI_rvmgd ~ BDMI_pre$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_pre$ps_rvmgd ~ BDMI_pre$ps_logxret)
vixregresult <- rbind(vixregresult, data.frame(q ="fs_pre",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
rm(qmax,regresult_bdmi,regresult_bdmlci,regresult_bdmxlci,regresult_ps)

#covid
for (qmax in 1:4) {
  regresult_bdmi <- lm(subset(BDMI_post, VIX_sort == qmax)$BDMI_rvmgd ~ subset(BDMI_post, VIX_sort == qmax)$BDMI_logxret)
  regresult_bdmlci <- lm(subset(BDMI_post, VIX_sort == qmax)$BDMLCI_rvmgd ~ subset(BDMI_post, VIX_sort == qmax)$BDMLCI_logxret)
  regresult_bdmxlci <- lm(subset(BDMI_post, VIX_sort == qmax)$BDMXLCI_rvmgd ~ subset(BDMI_post, VIX_sort == qmax)$BDMXLCI_logxret)
  regresult_ps <- lm(subset(BDMI_post, VIX_sort == qmax)$ps_rvmgd ~ subset(BDMI_post, VIX_sort == qmax)$ps_logxret)
  vixregresult <- rbind(vixregresult, data.frame(q = qmax,
                                                 alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                 stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                 p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                 beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                 stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                 p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                 adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                 ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                 
                                                 alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                 p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                 beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                 stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                 p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                 adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                 ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                 
                                                 alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                 stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                 p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                 beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                 stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                 p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                 adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                 ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                 
                                                 alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                 stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                 p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                 beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                 stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                 p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                 adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                 ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}
regresult_bdmi <- lm(BDMI_post$BDMI_rvmgd ~ BDMI_post$BDMI_logxret)
regresult_bdmlci <- lm(BDMI_post$BDMLCI_rvmgd ~ BDMI_post$BDMLCI_logxret)
regresult_bdmxlci <- lm(BDMI_post$BDMXLCI_rvmgd ~ BDMI_post$BDMXLCI_logxret)
regresult_ps <- lm(BDMI_post$ps_rvmgd ~ BDMI_post$ps_logxret)
vixregresult <- rbind(vixregresult, data.frame(q ="fs_post",
                                               alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                               stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                               p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                               beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                               stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                               p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                               adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                               ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                               
                                               alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                               stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                               p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                               beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                               stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                               p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                               adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                               ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                               
                                               alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                               stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                               p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                               beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                               stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                               p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                               adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                               ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                               
                                               alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                               stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                               p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                               beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                               stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                               p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                               adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                               ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))



#volume-cap ratio as sentiment proxy
volcapregresult <- data.frame()
for (qmax in 1:2) {
  regresult_bdmi <- lm(subset(BDMI_ps, btcvchalf == qmax)$BDMI_rvmgd ~ subset(BDMI_ps, btcvchalf == qmax)$BDMI_logxret)
  regresult_bdmlci <- lm(subset(BDMI_ps, btcvchalf == qmax)$BDMLCI_rvmgd ~ subset(BDMI_ps, btcvchalf == qmax)$BDMLCI_logxret)
  regresult_bdmxlci <- lm(subset(BDMI_ps, btcvchalf == qmax)$BDMXLCI_rvmgd ~ subset(BDMI_ps, btcvchalf == qmax)$BDMXLCI_logxret)
  regresult_ps <- lm(subset(BDMI_ps, btcvchalf == qmax)$ps_rvmgd ~ subset(BDMI_ps, btcvchalf == qmax)$ps_logxret)
  volcapregresult <- rbind(volcapregresult, data.frame(q = qmax,
                                                       alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                       stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                       p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                       beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                       stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                       p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                       adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                       ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                       
                                                       alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                       stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                       p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                       beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                       stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                       p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                       adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                       ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                       
                                                       alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                       stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                       p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                       beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                       stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                       p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                       adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                       ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                       
                                                       alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                       stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                       p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                       beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                       stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                       p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                       adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                       ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
  
  regresult_bdmi <- lm(subset(BDMI_ps, ethvchalf == qmax)$BDMI_rvmgd ~ subset(BDMI_ps, ethvchalf == qmax)$BDMI_logxret)
  regresult_bdmlci <- lm(subset(BDMI_ps, ethvchalf == qmax)$BDMLCI_rvmgd ~ subset(BDMI_ps, ethvchalf == qmax)$BDMLCI_logxret)
  regresult_bdmxlci <- lm(subset(BDMI_ps, ethvchalf == qmax)$BDMXLCI_rvmgd ~ subset(BDMI_ps, ethvchalf == qmax)$BDMXLCI_logxret)
  regresult_ps <- lm(subset(BDMI_ps, ethvchalf == qmax)$ps_rvmgd ~ subset(BDMI_ps, ethvchalf == qmax)$ps_logxret)
  volcapregresult <- rbind(volcapregresult, data.frame(q = qmax,
                                                       alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 1],
                                                       stderr_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 2],
                                                       p_alpha_bdmi = summary(regresult_bdmi)$coefficients[1, 4],
                                                       beta_bdmi = summary(regresult_bdmi)$coefficients[2, 1],
                                                       stderr_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 2],
                                                       p_beta_bdmi = summary(regresult_bdmi)$coefficients[2, 4],
                                                       adjrsq_bdmi = summary(regresult_bdmi)$adj.r.squared,
                                                       ar_bdmi = summary(regresult_bdmi)$coefficients[1, 1] / sd(resid(regresult_bdmi)),
                                                       
                                                       alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1],
                                                       stderr_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 2],
                                                       p_alpha_bdmlci = summary(regresult_bdmlci)$coefficients[1, 4],
                                                       beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 1],
                                                       stderr_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 2],
                                                       p_beta_bdmlci = summary(regresult_bdmlci)$coefficients[2, 4],
                                                       adjrsq_bdmlci = summary(regresult_bdmlci)$adj.r.squared,
                                                       ar_bdmlci = summary(regresult_bdmlci)$coefficients[1, 1] / sd(resid(regresult_bdmlci)),
                                                       
                                                       alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1],
                                                       stderr_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 2],
                                                       p_alpha_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 4],
                                                       beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 1],
                                                       stderr_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 2],
                                                       p_beta_bdmxlci = summary(regresult_bdmxlci)$coefficients[2, 4],
                                                       adjrsq_bdmxlci = summary(regresult_bdmxlci)$adj.r.squared,
                                                       ar_bdmxlci = summary(regresult_bdmxlci)$coefficients[1, 1] / sd(resid(regresult_bdmxlci)),
                                                       
                                                       alpha_ps = summary(regresult_ps)$coefficients[1, 1],
                                                       stderr_alpha_ps = summary(regresult_ps)$coefficients[1, 2],
                                                       p_alpha_ps = summary(regresult_ps)$coefficients[1, 4],
                                                       beta_ps = summary(regresult_ps)$coefficients[2, 1],
                                                       stderr_beta_ps = summary(regresult_ps)$coefficients[2, 2],
                                                       p_beta_ps = summary(regresult_ps)$coefficients[2, 4],
                                                       adjrsq_ps = summary(regresult_ps)$adj.r.squared,
                                                       ar_ps = summary(regresult_ps)$coefficients[1, 1] / sd(resid(regresult_ps))))
}


cor(subset(BDMI_ps,btcvcsort==4)$spread,subset(BDMI_ps,btcvcsort==4)$btcvolcap)
cor.test(subset(BDMI_ps,btcvcsort==4)$spread,subset(BDMI_ps,btcvcsort==4)$btcvolcap)


#ratio sorts
vcsort<-data.frame()
for (q in 1:2) {
  ss<-subset(BDMI_ps,btcvchalf==q)
  vcsort<-rbind(vcsort,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),
                                  BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),
                                  BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),
                                  ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd)))
}
rm(ss,q)
vcsort<-rbind(vcsort,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_ps,btcvchalf==2)$BDMI_logxret-subset(BDMI_ps,btcvchalf==1)$BDMI_logxret)$statistic,
                                BDMI_rvmgd=t.test(subset(BDMI_ps,btcvchalf==2)$BDMI_rvmgd-subset(BDMI_ps,btcvchalf==1)$BDMI_rvmgd)$statistic,
                                BDMLCI_xret=t.test(subset(BDMI_ps,btcvchalf==2)$BDMLCI_logxret-subset(BDMI_ps,btcvchalf==1)$BDMLCI_logxret)$statistic,
                                BDMLCI_rvmgd=t.test(subset(BDMI_ps,btcvchalf==2)$BDMLCI_rvmgd-subset(BDMI_ps,btcvchalf==1)$BDMLCI_rvmgd)$statistic,
                                BDMXLCI_xret=t.test(subset(BDMI_ps,btcvchalf==2)$BDMXLCI_logxret-subset(BDMI_ps,btcvchalf==1)$BDMXLCI_logxret)$statistic,
                                BDMXLCI_rvmgd=t.test(subset(BDMI_ps,btcvchalf==2)$BDMXLCI_rvmgd-subset(BDMI_ps,btcvchalf==1)$BDMXLCI_rvmgd)$statistic,
                                ps_xret=t.test(subset(BDMI_ps,btcvchalf==2)$ps_logxret-subset(BDMI_ps,btcvchalf==1)$ps_logxret)$statistic,
                                ps_rvmgd=t.test(subset(BDMI_ps,btcvchalf==2)$ps_rvmgd-subset(BDMI_ps,btcvchalf==1)$ps_rvmgd)$statistic))



for (q in 1:2) {
  ss<-subset(BDMI_ps,ethvchalf==q)
  vcsort<-rbind(vcsort,data.frame(q=q,BDMI_xret=mean(ss$BDMI_logxret),BDMI_rvmgd=mean(ss$BDMI_rvmgd),
                                  BDMLCI_xret=mean(ss$BDMLCI_logxret),BDMLCI_rvmgd=mean(ss$BDMLCI_rvmgd),
                                  BDMXLCI_xret=mean(ss$BDMXLCI_logxret),BDMXLCI_rvmgd=mean(ss$BDMXLCI_rvmgd),
                                  ps_xret=mean(ss$ps_logxret),ps_rvmgd=mean(ss$ps_rvmgd)))
}
rm(ss,q)
vcsort<-rbind(vcsort,data.frame(q="t-stat",BDMI_xret=t.test(subset(BDMI_ps,btcvchalf==2)$BDMI_logxret-subset(BDMI_ps,btcvchalf==1)$BDMI_logxret)$statistic,
                                BDMI_rvmgd=t.test(subset(BDMI_ps,btcvchalf==2)$BDMI_rvmgd-subset(BDMI_ps,btcvchalf==1)$BDMI_rvmgd)$statistic,
                                BDMLCI_xret=t.test(subset(BDMI_ps,btcvchalf==2)$BDMLCI_logxret-subset(BDMI_ps,btcvchalf==1)$BDMLCI_logxret)$statistic,
                                BDMLCI_rvmgd=t.test(subset(BDMI_ps,btcvchalf==2)$BDMLCI_rvmgd-subset(BDMI_ps,btcvchalf==1)$BDMLCI_rvmgd)$statistic,
                                BDMXLCI_xret=t.test(subset(BDMI_ps,btcvchalf==2)$BDMXLCI_logxret-subset(BDMI_ps,btcvchalf==1)$BDMXLCI_logxret)$statistic,
                                BDMXLCI_rvmgd=t.test(subset(BDMI_ps,btcvchalf==2)$BDMXLCI_rvmgd-subset(BDMI_ps,btcvchalf==1)$BDMXLCI_rvmgd)$statistic,
                                ps_xret=t.test(subset(BDMI_ps,btcvchalf==2)$ps_logxret-subset(BDMI_ps,btcvchalf==1)$ps_logxret)$statistic,
                                ps_rvmgd=t.test(subset(BDMI_ps,btcvchalf==2)$ps_rvmgd-subset(BDMI_ps,btcvchalf==1)$ps_rvmgd)$statistic))

